最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Golang函數(shù)式編程深入分析實例

 更新時間:2023年01月10日 11:00:24   作者:夢想畫家  
習(xí)慣與函數(shù)式編程語言的開發(fā)者,會認為for循環(huán)和if判斷語句是冗長的代碼,通過使用map和filter處理集合元素讓代碼更可讀。本文介紹Go閉包實現(xiàn)集合轉(zhuǎn)換和過濾功能

定義集合功能函數(shù)

首先定義用于測試的結(jié)構(gòu)體WorkWith

// WorkWith is the struct we'll
// be implementing collections for
type WorkWith struct {
	Data    string
	Version int
}

針對該結(jié)構(gòu)體定義filter和map函數(shù):

// 基于判斷函數(shù)過濾集合,返回符合條件的集合元素
func Filter(ws []WorkWith, f func(w WorkWith) bool) []WorkWith {
	// depending on results, smaller size for result
	// is len == 0
	result := make([]WorkWith, 0)
	for _, w := range ws {
		if f(w) {
			result = append(result, w)
		}
	}
	return result
}
// 基于轉(zhuǎn)換函數(shù)轉(zhuǎn)換集合元素,返回集合的元素為轉(zhuǎn)換后的元素
func Map(ws []WorkWith, f func(w WorkWith) WorkWith) []WorkWith {
	// the result should always be the same
	// length
	result := make([]WorkWith, len(ws))
	for pos, w := range ws {
		newW := f(w)
		result[pos] = newW
	}
	return result
}

實現(xiàn)具體功能函數(shù)

import "strings"
// LowerCaseData does a ToLower to the
// Data string of a WorkWith
func LowerCaseData(w WorkWith) WorkWith {
	w.Data = strings.ToLower(w.Data)
	return w
}
// IncrementVersion increments a WorkWiths
// Version
func IncrementVersion(w WorkWith) WorkWith {
	w.Version++
	return w
}
// OldVersion returns a closures
// that validates the version is greater than
// the specified amount
func OldVersion(v int) func(w WorkWith) bool {
	return func(w WorkWith) bool {
		return w.Version >= v
	}
}

上面定義了三個函數(shù),LowerCaseData修改WorkWith中Data值為小寫形式,IncrementVersion讓W(xué)orkWith中版本增加1,OldVersion基于參數(shù)過濾版本。

測試集合功能

定義測試用例文件:

import (
	"fmt"
	"testing"
)
func TestMap(t *testing.T) {
	ws := []WorkWith{
		{"Example", 1},
		{"Example 2", 2},
	}
	fmt.Printf("Initial list: %#v\n", ws)
	// first lower case the list
	ws = Map(ws, LowerCaseData)
	fmt.Printf("After LowerCaseData Map: %#v\n", ws)
	// next increment all versions
	ws = Map(ws, IncrementVersion)
	fmt.Printf("After IncrementVersion Map: %#v\n", ws)
	// lastly remove all versions older than 3
	ws = Filter(ws, OldVersion(3))
	fmt.Printf("After OldVersion Filter: %#v\n", ws)
}

運行 go test . -v

輸出結(jié)果如下:

Initial list: []collections.WorkWith{collections.WorkWith{Data:"Example", Version:1}, collections.WorkWith{Data:"Example 2", Version:2}}

After LowerCaseData Map: []collections.WorkWith{collections.WorkWith{Data:"example", Version:1}, collections.WorkWith{Data:"example 2", Version:2}}

After IncrementVersion Map: []collections.WorkWith{collections.WorkWith{Data:"example", Version:2}, collections.WorkWith{Data:"example 2", Version:3}}

After OldVersion Filter: []collections.WorkWith{collections.WorkWith{Data:"example 2", Version:3}}

上面示例中,我們注意到函數(shù)都沒有返回任何error對象,這遵循函數(shù)式編程思想,盡可能讓函數(shù)純粹:不修改原集合元素,即對原集合無副作用,而是生成新的集合。如果需要對集合應(yīng)用多個功能,那么這種模式能夠省去很多麻煩,并且測試也很簡單。我們還可以將映射和過濾器鏈接在一起,讓代碼更簡潔可讀。

	ws := []WorkWith{
		{"Example", 1},
		{"Example 2", 2},
	}
	fmt.Printf("Initial list: %#v\n", ws)
	result := Filter(Map(Map(ws, LowerCaseData), IncrementVersion), OldVersion(3))
	fmt.Printf("After OldVersion Filter: %#v\n", result)

如果功能函數(shù)定義為集合類型的方法,并返回集合類型,則上述代碼會更優(yōu)雅。

泛型實現(xiàn)

上面代碼僅能在特定類型上使用,我們自然想實現(xiàn)泛型函數(shù),下面通過一個簡單示例進行說明:

func map2[T, U any](data []T, f func(T) U) []U {
    res := make([]U, 0, len(data))
    for _, e := range data {
        res = append(res, f(e))
    }
    return res
}

該函數(shù)接收類型T,轉(zhuǎn)換后返回類型U,當然兩者類型也可以一樣。下面測試函數(shù)功能:

    // 字符串轉(zhuǎn)大寫
    words := []string{"war", "cup", "water", "tree", "storm"}
    result := map2(words, func(s string) string {
        return strings.ToUpper(s)
    })
    fmt.Println(result)
    // 生成原集合元素的平方集合
    fmt.Println("-------------------")
    numbers := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
    squares := map2(numbers, func(n int) int {
        return n * n
    })
    fmt.Println(squares)
    // 數(shù)值轉(zhuǎn)為字符串
    fmt.Println("-------------------")
    as_strings := map2(numbers, func(n int) string {
        return strconv.Itoa(n)
    })
    fmt.Printf("%q", as_strings)

到此這篇關(guān)于Golang函數(shù)式編程深入分析實例的文章就介紹到這了,更多相關(guān)Go函數(shù)式編程內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • CSP communicating sequential processes并發(fā)模型

    CSP communicating sequential processes并發(fā)模型

    這篇文章主要為大家介紹了CSP communicating sequential processes并發(fā)模型,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-05-05
  • Golang爬蟲框架 colly的使用

    Golang爬蟲框架 colly的使用

    本文主要介紹了Golang爬蟲框架 colly的使用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • VScode下配置Go語言開發(fā)環(huán)境(2023最新)

    VScode下配置Go語言開發(fā)環(huán)境(2023最新)

    在VSCode中配置Golang開發(fā)環(huán)境是非常簡單的,本文主要記錄了Go的安裝,以及給vscode配置Go的環(huán)境,具有一定的參考價值,感興趣的可以了解一下
    2023-10-10
  • Go 常量基礎(chǔ)概念(聲明更改只讀)

    Go 常量基礎(chǔ)概念(聲明更改只讀)

    這篇文章主要為大家介紹了Go常量基礎(chǔ)概念包括常量的聲明更改只讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-08-08
  • go chan基本使用詳解

    go chan基本使用詳解

    本文主要介紹了go chan基本使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • 搭建Go語言的ORM框架Gorm的具體步驟(從Java到go)

    搭建Go語言的ORM框架Gorm的具體步驟(從Java到go)

    很多朋友不知道如何使用Goland軟件,搭建一個ORM框架GORM,今天小編給大家分享一篇教程關(guān)于搭建Go語言的ORM框架Gorm的具體步驟(從Java到go),感興趣的朋友跟隨小編一起學(xué)習(xí)下吧
    2022-09-09
  • go基礎(chǔ)語法50問及方法詳解

    go基礎(chǔ)語法50問及方法詳解

    這篇文章主要為大家介紹了go基礎(chǔ)語法50問及方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-01-01
  • 解決 Golang VS Code 插件下載安裝失敗的問題

    解決 Golang VS Code 插件下載安裝失敗的問題

    這篇文章主要介紹了解決 Golang VS Code 插件下載安裝失敗,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-05-05
  • golang語言中for循環(huán)語句用法實例

    golang語言中for循環(huán)語句用法實例

    這篇文章主要介紹了golang語言中for循環(huán)語句用法,實例分析了for循環(huán)遍歷的使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-01-01
  • golang 調(diào)用c語言動態(tài)庫方式實現(xiàn)

    golang 調(diào)用c語言動態(tài)庫方式實現(xiàn)

    本文主要介紹了golang 調(diào)用c語言動態(tài)庫方式實現(xiàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-12-12

最新評論

惠州市| 佛教| 铁力市| 集安市| 钟祥市| 萨迦县| 石嘴山市| 阜南县| 石泉县| 镇坪县| 红桥区| 台江县| 洱源县| 牡丹江市| 含山县| 呼玛县| 日喀则市| 博乐市| 扶风县| 建瓯市| 肃宁县| 丹棱县| 普安县| 镶黄旗| 桓仁| 大连市| 大理市| 桦甸市| 永清县| 衡山县| 阿图什市| 绥棱县| 宁乡县| 增城市| 西安市| 秀山| 黔东| 彭水| 庆安县| 甘德县| 会东县|