Skip to content

Commit

Permalink
feat: add functional programming example
Browse files Browse the repository at this point in the history
  • Loading branch information
trmaphi committed Mar 31, 2022
1 parent 315933a commit 1d8c397
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 0 deletions.
28 changes: 28 additions & 0 deletions functional-programming.js/cachable-function.js
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"))
50 changes: 50 additions & 0 deletions functional-programming.js/first-class-function.js
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);
16 changes: 16 additions & 0 deletions functional-programming.js/pure-function.js
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);

71 changes: 71 additions & 0 deletions trie-go/trie.go
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])
}
}
}

0 comments on commit 1d8c397

Please sign in to comment.