diff --git a/functional-programming.js/cachable-function.js b/functional-programming.js/cachable-function.js new file mode 100644 index 0000000..e116902 --- /dev/null +++ b/functional-programming.js/cachable-function.js @@ -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")) \ No newline at end of file diff --git a/functional-programming.js/first-class-function.js b/functional-programming.js/first-class-function.js new file mode 100644 index 0000000..3d132cc --- /dev/null +++ b/functional-programming.js/first-class-function.js @@ -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); \ No newline at end of file diff --git a/functional-programming.js/pure-function.js b/functional-programming.js/pure-function.js new file mode 100644 index 0000000..5ef380a --- /dev/null +++ b/functional-programming.js/pure-function.js @@ -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); + diff --git a/trie-go/trie.go b/trie-go/trie.go new file mode 100644 index 0000000..ba30701 --- /dev/null +++ b/trie-go/trie.go @@ -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]) + } + } +}