Skip to content

Instantly share code, notes, and snippets.

View Svetixbot's full-sized avatar
♥️
Code with kindness

Svetlana Marina Svetixbot

♥️
Code with kindness
View GitHub Profile
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)
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);
/*
* 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) ||
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
val isAlienLanguage = "([a-zA-Z]+) (is) (.*)".r
/***
* glob is I
*/
def parseAlien(statement: String): PartialFunction[String, AlienLanguageStatement] = {
case isAlienLanguage(word, _, roman) => AlienLanguageStatement(word, roman)
}
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)
}
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.
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()
}
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()
@Svetixbot
Svetixbot / list.hs
Last active August 29, 2015 14:16
Implementation of some of the functions from Modules #learnyouahaskell
-- 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