Skip to content

Instantly share code, notes, and snippets.

View 3v0k4's full-sized avatar

Riccardo 3v0k4

View GitHub Profile
const Result = require('folktale/result')
const R = require('ramda')
const first = xs => Result.fromNullable(xs[0])
const parse = s => Result.try(() => JSON.parse(s))
const upper = s => s.toUpperCase()
const shout = s => s.concat('!')
const Result = require('folktale/result')
const first = xs => Result.fromNullable(xs[0])
const parse = s => Result.try(() => JSON.parse(s))
const upper = s => s.toUpperCase()
const shout = s => s.concat('!')
const Right = x =>
({
x,
map: f => Right(f(x)),
inspect: _ => `Right(${x})`,
isLeft: _ => false,
chain: f => f(x),
})
const Left = x =>
const Right = x =>
({
x,
map: f => Right(f(x)),
inspect: _ => `Right(${x})`,
isLeft: _ => false,
chain: f => f(x),
})
const Left = x =>
const parse = s => tryCatch(() => JSON.parse(s))
const first = xs => fromNullable('empty array', xs[0])
const upper = s => s.toUpperCase()
const shout = s => s.concat('!')
const toString = i => String(i) // <-- added this
const functional = s =>
Right(s)
.chain(parse)
.chain(first)
.map(toString) // <-- added this
.chain(format)
const functional2 = ss => ss.map(functional) // <-- added this
// tryCatch :: Function => Either(String)
const tryCatch = f => {
try {
return Right(f())
} catch (e) {
return Left('could not parse')
}
}
// parse :: String -> Either [String]
// upper :: String -> String
const upper = s => s.toUpperCase()
// shout :: String -> String
const shout = s => s.concat('!')
// format :: String => Either(String)
const format = s =>
Right(s)
.map(upper)
Right(1).map(x =>
Right(2).map(y => x + y)) // Right(Right(3))
Right(1).chain(x =>
Right(2).map(y => x + y)) // Right(3)
const Right = x =>
({
x,
map: f => Right(f(x)),
chain: f => f(x)
})
const Left = x =>
({
x,