亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 TypeScript/JavaScript 中的函數式編程將元素推送到 Map 內的數組?

如何使用 TypeScript/JavaScript 中的函數式編程將元素推送到 Map 內的數組?

哈士奇WWW 2022-06-09 16:53:16
我剛剛開始了從 OOP 背景到學習 FP 的旅程,以及從編寫普通 TypeScript(命令式?)遷移到功能 TypeScript 代碼的過程。不幸的是,我已經在努力弄清楚如何將其更改為功能代碼:const foos: Map<  string,  Bar[]> = new Map();export const addBar = (  key: string,  bar: Bar) => {  const foo = foos.get(key);  if (foo) {    foo.push(bar);  } else {    foos.set(key, [bar]);  }};我了解如何在數組上使用 .map .filter .concat ,但是如何處理包含數組的 Map 呢?關于 foos Map 我猜 Map 本身需要是只讀的,而且里面的 Bar 數組也是如此,所以 .set .push 是不可能的。但是,如果我不能在 Map 上調用 .set 因為它是只讀的,那么使用 Map 是否有意義,或者我應該只使用一個對象?如果沒有可變性,如何將元素推送到 Map 值內的數組(或者如果鍵不存在,則使用數組創建一個新映射,如上面的代碼中所示)?這是否足夠高效,因為我需要每隔幾秒向數組添加一個新元素,每次發生更改時復制整個地圖(包括其許多數組)的不可變方式不會比我執行得更糟嗎?剛剛像您通常所做的那樣對數組進行了變異?
查看完整描述

2 回答

?
FFIVE

TA貢獻1797條經驗 獲得超6個贊

您根本無法使用本機Map,因為它僅提供命令式接口。


您可以使用開源庫,例如流行的 ImmutableJS。


或者您可以編寫自己的持久(不可變)數據結構。基本要求是您的數據結構提供的操作不會修改輸入。相反,每個操作都會返回一個新的數據結構 -


const PersistentMap =

  { create: () =>

      ({})

  , set: (t = {}, key, value) =>

      ({ ...t, [key]: value })      // <-- immutable operation

  }

我們首先看一張empty地圖,一個set操作的結果,然后確保empty地圖沒有被修改——


const empty =

  PersistentMap.create()


console.log

  ( empty

  , PersistentMap.set(empty, "hello", "world")

  , empty

  )


// {}

// { hello: "world" }

// {}

現在讓我們看一個新的中間狀態,m1。每次我們看到set返回一個新的持久映射并且不修改輸入 -


const m1 =

  PersistentMap.set(empty, "hello", "earth")


console.log

  ( m1

  , PersistentMap.set(m1, "stay", "inside")

  , m1

  )

// { hello: "earth" }

// { hello: "earth", stay: "inside" }

// { hello: "earth" }

現在回答你的問題,我們可以添加一個push操作到我們的PersitentMap- 我們只需要確保我們不修改輸入。這是一種可能的實現-


const PersistentMap =

  { // ...


  , push: (t = {}, key, value) =>

      PersistentMap.set            // <-- immutable operation

        ( t

        , key

        , Array.isArray(t[key])

            ? [ ...t[key], value ] // <-- immutable operation

            : [ value ]

        )

  }

我們push在下面看到行動。請注意,m2也不會empty因此而改變 -


const m2 =

  PersistentMap.push(empty, "fruits", "apple")


console.log

  ( m2

  , PersistentMap.push(m2, "fruits", "peach")

  , m2

  , empty

  )


// { fruits: [ "apple" ] }

// { fruits: [ "apple", "peach" ] }

// { fruits: [ "apple" ] }

// {}

展開下面的代碼段以在您自己的瀏覽器中驗證結果

const PersistentMap =

  { create: () =>

      ({})

  , set: (t = {}, key, value) =>

      ({ ...t, [key]: value })

  , push: (t = {}, key, value) =>

      PersistentMap.set

        ( t

        , key

        , Array.isArray(t[key])

            ? [ ...t[key], value ]

            : [ value ]

        )

  }


const empty =

  PersistentMap.create()


console.log

  ( empty

  , PersistentMap.set(empty, "hello", "world")

  , empty

  )

// {}

// { hello: "world" }

// {}


const m1 =

  PersistentMap.set(empty, "hello", "earth")


console.log

  ( m1

  , PersistentMap.set(m1, "stay", "inside")

  , m1

  )

// { hello: "earth" }

// { hello: "earth", stay: "inside" }

// { hello: "earth" }


const m2 =

  PersistentMap.push(empty, "fruits", "apple")


console.log

  ( m2

  , PersistentMap.push(m2, "fruits", "peach")

  , m2

  , empty

  )

// { fruits: [ "apple" ] }

// { fruits: [ "apple", "peach" ] }

// { fruits: [ "apple" ] }

// {}


查看完整回答
反對 回復 2022-06-09
?
Cats萌萌

TA貢獻1805條經驗 獲得超9個贊

我認為這取決于你想要達到的目標。如果您希望您的代碼是可測試的,FP 并不總是意味著只需要編寫函數,您仍然可以使用類,但是如果您有一段復雜的代碼要單獨測試,您可以導出該代碼段進行測試,并且它看起來像這樣:


// types.ts

type FooDis = Record<string, object[]>;


// addBarToFoos.ts

export const addBarToFoos = (foos: FooDis) => (key: string, bar: object): FooDis {

  foos = {

    ...foos,

    [key]: [

      ...foos[key],

      bar

    ]

  };


  return foos;

}


// FooClass.ts 

export class FooClass {

  private foos: FooDis = {};


  addBar(key: string, bar: object) {

    this.foos = addBarToFoos(this.foos)(key, bar);

  }

}

這樣,“復雜”方法可以在沒有外部依賴的情況下單獨測試,并且您有一個使用該方法的實現。


查看完整回答
反對 回復 2022-06-09
  • 2 回答
  • 0 關注
  • 140 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號