Last active
March 27, 2019 21:01
-
-
Save fvilante/f236b57140800a128684053d5827069f to your computer and use it in GitHub Desktop.
TS Algorithms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Node_<T> { | |
readonly value: T | |
readonly childreen: ReadonlyArray<Node_<T>> | undefined | |
} | |
const state: Node_<string> = { | |
value: "foo", | |
childreen: [ | |
{ | |
value: "bar", | |
childreen: [{ | |
value: "foo", | |
childreen: undefined | |
}, | |
{ | |
value: "bar", | |
childreen: undefined | |
}] | |
}] | |
} | |
const mapNode = <T, U>(node: Node_<T>, mapperFn: (val:T) => U):Node_<U> => | |
({ | |
value: mapperFn(node.value), | |
childreen: (node.childreen === undefined) | |
? undefined | |
: node.childreen.map( nextNode => mapNode(nextNode, mapperFn)) | |
}) |
Toda mutabilidade por definicao é um side-effect.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uma versao mutavel (side-effect) fica assim