Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 面向对象到功能—;从日常问题中学习_Typescript_Functional Programming_Fp Ts - Fatal编程技术网

Typescript 面向对象到功能—;从日常问题中学习

Typescript 面向对象到功能—;从日常问题中学习,typescript,functional-programming,fp-ts,Typescript,Functional Programming,Fp Ts,我正要学习使用fp ts进行函数式编程,我只是在问自己,将这样的东西转换为函数范式的正确函数方式是什么: //OOP: interface Item { name: string; } class X { private readonly items: { [s:string]: Item[] } = {}; add(i: Item): Item { if(!this.items.hasOwnProperty(i.name)) this.items[i.na

我正要学习使用
fp ts
进行函数式编程,我只是在问自己,将这样的东西转换为函数范式的正确函数方式是什么:

//OOP:

interface Item {
  name: string;
}

class X {
  private readonly items: { [s:string]: Item[] } = {};

  add(i: Item): Item {
    if(!this.items.hasOwnProperty(i.name))
      this.items[i.name] = [];

    if(this.items[i.name].indexOf(i) < 0)    
      this.items[i.name].push(i);

    return i;
  }
}

由于
add()
操作涉及状态的修改,它是否应该依赖于
IO
?如果
add
返回一个新的状态对象,那么它是一个纯函数,所以不需要使用IO?所以我在这里提出的问题可能有点宽泛,但是:这里推荐的技术/模式是什么

由于add()操作涉及状态的修改,它应该依赖IO吗

是的,add()不会返回任何内容,但具有状态效果,因此它应该返回
IO

如果add返回一个新的state对象,那么它是一个纯函数,所以不需要使用IO

这里推荐的技术/模式是什么

函数式程序员通常不惜一切代价避免可变状态

您试图实现的是一个写在多映射上的拷贝。为此,您不需要使用
fp ts
中的任何内容

type MyItem={name:string};
//我们已经使商店具有多态性
类型MyObj={[s:string]:Item[]};
//只有当您想要公开独立于实现的api时,才需要这样做。
export const empty={};
//在o之前输入i,以防我们以后需要咖喱,o将改变得更多。
常量添加=(k:string,v:Item,o:MyObj)=>
//滥用spread操作符来创建新对象和新数组
({…o[k]:[v,…(o[k]| |[])]});
//为您的物品案例专门化
export const addMyItem=(i:MyItem,o:MyObj)=>add(i.name,i,o);
然后,您可以执行以下操作:

const a=addMyItem({name:“test”},addMyItem({name:“test”},空));

看看redux js是如何工作的
import * as O from 'fp-ts/es6/Option';
import * as E from 'fp-ts/es6/Either';

// using interfaces from above

interface state {
  items: { [s:string]: Item[] }
}

export const createState = (): State => ({ items: {} });


export const add = (s: State, i: Item) => pipe(
  // using IO here?
  E.fromPredicate(
    () => s.hasOwnProperty(i.name),
    () => []
  )
  
    
)

// to use it:

import { createState, add } from './fx';

let state = createState();

// update
state = add(state, {name: 'foo'})