-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add functional programming example
- Loading branch information
Showing
4 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const sleep = (milliseconds) => { | ||
const end = Date.now() + milliseconds | ||
while (Date.now() < end) continue | ||
} | ||
|
||
// Run so slow | ||
function longRunningFn(args) { | ||
sleep(3000) | ||
return `longRunningFn result ${args}` | ||
} | ||
|
||
const memoize = (f) => { | ||
const cache = {}; | ||
|
||
return (...args) => { | ||
const argStr = JSON.stringify(args); | ||
cache[argStr] = cache[argStr] || f(...args); | ||
return cache[argStr]; Å | ||
}; | ||
}; | ||
|
||
const memoizedlongRunningFn = memoize(longRunningFn); | ||
|
||
console.log(memoizedlongRunningFn("abc")) | ||
console.log(memoizedlongRunningFn("abc")) | ||
|
||
// console.log(longRunningFn("abc")) | ||
// console.log(longRunningFn("abc")) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
const firstClassFn = function (str) { | ||
console.log(str); | ||
} | ||
|
||
firstClassFn("Ha Vi"); | ||
console.log(typeof firstClassFn) | ||
console.log(firstClassFn instanceof Object) | ||
|
||
// Use synchronous callback | ||
const useCallBackFn = function (callBackFn) { | ||
callBackFn(); | ||
} | ||
|
||
useCallBackFn(function () { | ||
console.log("Ha vi in callback") | ||
}) | ||
|
||
// Use asynchronous callback | ||
const useAsyncFn = function (asyncFn) { | ||
console.log("Ha vi call immediately") | ||
setTimeout(asyncFn, 2000) | ||
} | ||
|
||
useAsyncFn(function () { | ||
console.log("Ha vi call after 2s") | ||
}) | ||
|
||
// | ||
const isTrue = function (value, callback) { | ||
if (value === true) { | ||
callback(null, "Value was true."); | ||
} | ||
else { | ||
callback(new Error("Value is not true!")); | ||
} | ||
} | ||
|
||
const callback = function (error, retval) { | ||
if (error) { | ||
console.log(error); | ||
return; | ||
} | ||
console.log(retval); | ||
} | ||
|
||
// Note: when calling the same asynchronous function twice like this, you are in a race condition. | ||
// You have no way of knowing for certain which callback will be called first when calling the functions in this manner. | ||
|
||
isTrue(false, callback); | ||
isTrue(true, callback); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const pureFn = (arr) => {return arr[0]}; | ||
const arr = [2,3,4]; | ||
console.log(pureFn(arr)); | ||
|
||
const impureFn = function(arr) { | ||
arr.forEach((element, index) => { | ||
arr[index] = element + 1; | ||
}) | ||
|
||
return arr; | ||
} | ||
|
||
console.log(arr); | ||
console.log(impureFn(arr)); | ||
console.log(arr); | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
const ( | ||
//ALBHABET_SIZE total characters in english alphabet | ||
ALBHABET_SIZE = 26 | ||
) | ||
|
||
type trieNode struct { | ||
childrens [ALBHABET_SIZE]*trieNode | ||
isWordEnd bool | ||
} | ||
|
||
type trie struct { | ||
root *trieNode | ||
} | ||
|
||
func initTrie() *trie { | ||
return &trie{ | ||
root: &trieNode{}, | ||
} | ||
} | ||
|
||
func (t *trie) insert(word string) { | ||
wordLength := len(word) | ||
current := t.root | ||
for i := 0; i < wordLength; i++ { | ||
index := word[i] - 'a' | ||
if current.childrens[index] == nil { | ||
current.childrens[index] = &trieNode{} | ||
} | ||
current = current.childrens[index] | ||
} | ||
current.isWordEnd = true | ||
} | ||
|
||
func (t *trie) find(word string) bool { | ||
wordLength := len(word) | ||
current := t.root | ||
for i := 0; i < wordLength; i++ { | ||
index := word[i] - 'a' | ||
if current.childrens[index] == nil { | ||
return false | ||
} | ||
current = current.childrens[index] | ||
} | ||
if current.isWordEnd { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func main() { | ||
trie := initTrie() | ||
words := []string{"sam", "john", "tim", "jose", "rose", | ||
"cat", "dog", "dogg", "roses"} | ||
for i := 0; i < len(words); i++ { | ||
trie.insert(words[i]) | ||
} | ||
wordsToFind := []string{"sam", "john", "tim", "jose", "rose", | ||
"cat", "dog", "dogg", "roses", "rosess", "ans", "san"} | ||
for i := 0; i < len(wordsToFind); i++ { | ||
found := trie.find(wordsToFind[i]) | ||
if found { | ||
fmt.Printf("Word \"%s\" found in trie\n", wordsToFind[i]) | ||
} else { | ||
fmt.Printf("Word \"%s\" not found in trie\n", wordsToFind[i]) | ||
} | ||
} | ||
} |