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
-- places a separator between each element of an array and produces new array | |
intersperse' :: a -> [a] -> [a] | |
intersperse' _ [] = [] | |
intersperse' a (x:xs) = x : a : intersperse' a xs | |
-- takes a list of lists and a list. | |
-- It then inserts that list in between all those lists and then flattens the result | |
intercalate' :: [a] -> [[a]] -> [a] | |
intercalate' _ [] = [] | |
intercalate' l1 (x:[]) = x |
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
import operator | |
from BanditGame import BanditGame | |
ba = BanditGame() | |
results = {} | |
for arm in range(1, 6): | |
results[arm] = 0 | |
for game in range(20): | |
startValue = ba.money() |
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
use std::ops::BitAnd; | |
use std::cmp::PartialEq; | |
use std::num::{One, Zero}; | |
fn even<T>(value: T) -> bool | |
where T: BitAnd<T, Output = T> + PartialEq + One + Zero { | |
value & T::one() == T::zero() | |
} |
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
1. this: | |
if a.is_none() || c == std::u64::MAX { | |
None | |
} else { | |
a.unwrap().checked_add(c) | |
} | |
can be replaces with (not sure if it compiles though...): | |
a.and_then(|value| value.checked_add(c)) | |
2. Once you start using Option for something in your code, you have to use Option everywhere, otherwise composition looks horrible. |
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
const pool = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" | |
func generateId(n int) string { | |
b := make([]byte, n) | |
for i := range b { | |
b[i] = pool[rand.Intn(len(pool))] | |
} | |
return string(b) | |
} |
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
val isAlienLanguage = "([a-zA-Z]+) (is) (.*)".r | |
/*** | |
* glob is I | |
*/ | |
def parseAlien(statement: String): PartialFunction[String, AlienLanguageStatement] = { | |
case isAlienLanguage(word, _, roman) => AlienLanguageStatement(word, roman) | |
} | |
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
import unfiltered.netty.cycle.Plan.Intent | |
import unfiltered.netty.{ServerErrorResponse, cycle} | |
import unfiltered.request.{GET, POST, Path} | |
import unfiltered.response.{BadRequest, ResponseString} | |
import unfiltered.Cycle | |
import unfiltered.directives._ | |
import Directives._ | |
object Mom extends cycle.Plan |
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
/* | |
* There are 2 {validation} patterns. There is always place for both of them. How do I do the second one? | |
* 1. Validate against different validators and return the first failure. | |
* 2. Validate against different validators and return all the failures. | |
*/ | |
/* 1. Compose and terminate with first failure */ | |
const validateMinMagicNumber = (min_magic) => ( // accepts a number and returns string with the first error. | |
isEmpty(min_magic, 'Min Magic is required') || | |
failedParseNumber(min_magic) || |
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
d1, d2, d3 are functions to deconstruct the {data} | |
t1, t2, t3 are functions to transform each of the destructed pieces | |
<*> apply function from Applicative. | |
// The <*> function is from Applicative and enables apply array of functions to an argument. | |
// The A[x] would hold a value of computation of an exception/null in case something went wrong. | |
var resultOfDestruct = [d1, d2, d3] <*> data // [A[a], A[b], A[c]] | |
var resultOfTransform = (resultDestruct) zip [t1, t2, t3] (<*>) // [A[a1], A[b1], A[c1]] | |
var transformation = resultOfTransform.filterFailures.Reduce(merge); |
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
d1, d2, d3 are functions to deconstruct the {data} | |
t1, t2, t3 are functions to transform each of the destructed pieces | |
var result = for { | |
real_a <- data | d1 | t1 | |
real_b <- data | d2 | t2 | |
real_c <- data | d3 | t3 | |
} yield (real_a, real_b, real_c) | |
result: Exception \/ (real_a, real_b, real_c) |