Skip to content

Instantly share code, notes, and snippets.

@taroyanaka
taroyanaka / StreamToString.go
Created October 18, 2018 08:11 — forked from tejainece/StreamToString.go
Golang: io.Reader stream to string or byte slice
import "bytes"
func StreamToByte(stream io.Reader) []byte {
buf := new(bytes.Buffer)
buf.ReadFrom(stream)
return buf.Bytes()
}
func StreamToString(stream io.Reader) string {
buf := new(bytes.Buffer)
@taroyanaka
taroyanaka / permutations.js
Created October 23, 2018 04:26 — forked from CrossEye/permutations.js
Find permutations of a list of (distinct) values. Uses Ramda
// https://codepen.io/taroyanaka/pen/bmxoMw?editors=0011
const R = require('ramda');
const permutations = (tokens, subperms = [[]]) =>
R.isEmpty(tokens) ?
subperms :
R.addIndex(R.chain)((token, idx) => permutations(
R.remove(idx, 1, tokens),
R.map(R.append(token), subperms)
), tokens);
@taroyanaka
taroyanaka / ramda4.js
Created October 24, 2018 02:36 — forked from sebabouche/ramda4.js
ramda: has, path, propOr, pathOr, keys, values
import { has, path, propOr, pathOr, keys, values } from "ramda"
const one = {
a: "Bingo",
b: {
c: "Bingo",
},
}
const two = {
@taroyanaka
taroyanaka / ramda0.js
Created October 24, 2018 02:38 — forked from sebabouche/ramda0.js
ramda: compose, bboth, either, gte, prop, equals, __
import { compose, both, either, gte, prop, equals, __ } from "ramda"
const OUR_COUNTRY = "France"
const wasBornInCountry = compose(
equals(OUR_COUNTRY),
prop("birthCountry"),
)
const wasNaturalized = compose(
Boolean,
@taroyanaka
taroyanaka / ramda1.js
Created October 24, 2018 02:38 — forked from sebabouche/ramda1.js
ramda: map, filter, partial, partialRight, curry, __, pipe
import { map, filter, partial, partialRight, curry, __, pipe } from "ramda"
const publishedInYear = curry((year, book) => book.year === year)
const titlesForYear = curry((year, books) =>
pipe(
filter(publishedInYear(year)),
map(book => book.title),
)(books),
)
@taroyanaka
taroyanaka / ramda2.js
Created October 24, 2018 02:38 — forked from sebabouche/ramda2.js
ramda: ifElse, gte, lt, always, identity, whn, unless
import { ifElse, gte, lt, __, always, identity, when, unless } from "ramda"
// const forever21 = age => (age >= 21 ? 21 : age)
// const forever21 = ifElse(gte(__, 21), always(21), identity)
// const forever21 = unless(lt(__, 21), always(21))
const forever21 = when(gte(__, 21), always(21))
console.log("forever21(24)", forever21(24))
console.log("forever21(16)", forever21(16))
@taroyanaka
taroyanaka / ramda3.js
Created October 24, 2018 02:38 — forked from sebabouche/ramda3.js
ramda: cond, always, T, identity
import { cond, always, lte, gte, T, __, identity } from "ramda"
const water = cond([
[lte(__, 0), temp => `water freezes at ${temp}°C`],
[gte(__, 100), temp => `water boils at ${temp}°C`],
[T, temp => `nothing special happens at ${temp}°C`],
])
console.log(water(-222))
@taroyanaka
taroyanaka / ramda5.js
Created October 24, 2018 02:38 — forked from sebabouche/ramda5.js
ramda: assoc, assocPath, dissoc, dissocPath, omit, compose, inc, prop
import { assoc, assocPath, dissoc, dissocPath, omit, compose, inc, prop } from "ramda"
const original = {
a: "Bingo",
b: {
c: "Bingo",
d: "Bingo",
},
}
@taroyanaka
taroyanaka / ramda6.js
Created October 24, 2018 02:39 — forked from sebabouche/ramda6.js
ramda: objects (compose, inc, prop, assoc, evolve, always, add)
import { compose, inc, prop, assoc, evolve, always, add } from "ramda"
const nextAge = compose(
inc,
prop("age"),
)
console.log("nextAge", nextAge({ age: 21 }))
const celebrateBirthday = person => assoc("age", nextAge(person), person)
const person = { age: 24 }
console.log("celebrateBirthday", celebrateBirthday(person))
@taroyanaka
taroyanaka / ramda7.js
Created October 24, 2018 02:39 — forked from sebabouche/ramda7.js
ramda: arrays (nth, slice, contains, head, last, tail, init...)
import {
nth,
slice,
contains,
head,
last,
tail,
init,
length,
take,