Notebooks 2.0 is here.
Read the preview announcement
Platform
Resources
Pricing
Sign in
Get started
Heiker
Workspace
Fork
Published
Functional programming for your everyday javascript
By
Heiker
Edited
Apr 28, 2021
4 stars
Functional programming for your everyday javascript
Reduce: how and when
Partial application in javascript
Composition techniques
About Functors
Using the Maybe type in js
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
numbers
=
[
'99'
,
'104'
]
Insert cell
repeat_twice
=
{
return
function
(
str
)
{
return
str
.
repeat
(
2
)
;
}
}
Insert cell
Insert cell
function
map
(
fn
,
array
)
{
return
array
.
map
(
fn
)
;
}
Insert cell
map
(
repeat_twice
,
numbers
)
;
Insert cell
Insert cell
{
function
unary
(
fn
)
{
return
function
(
arg
)
{
return
fn
(
arg
)
;
}
}
const
safer_parseint
=
unary
(
parseInt
)
;
return
map
(
safer_parseint
,
numbers
)
;
}
Insert cell
Insert cell
Insert cell
function
get_env
(
)
{
return
fs
.
readFileSync
(
'.env'
,
'utf-8'
)
;
}
Insert cell
Insert cell
function
search_host
(
content
)
{
const
exp
=
new
RegExp
(
'^HOST='
)
;
const
lines
=
content
.
split
(
'\n'
)
;
return
lines
.
find
(
line
=>
exp
.
test
(
line
)
)
;
}
Insert cell
Insert cell
function
get_value
(
str
)
{
return
str
.
split
(
'='
)
[
1
]
;
}
Insert cell
Insert cell
Insert cell
get_value
(
search_host
(
get_env
(
)
)
)
;
Insert cell
Insert cell
test
(
ping
(
get_value
(
search_host
(
get_env
(
)
)
)
)
)
;
Insert cell
Insert cell
Insert cell
function
compose
(
...
fns
)
{
return
function
_composed
(
...
args
)
{
// Index of the last function
let
last
=
fns
.
length
-
1
;
// Call the last function
// with arguments of `_composed`
let
current_value
=
fns
[
last
--
]
(
...
args
)
;
// loop through the rest in the opposite direction
for
(
let
i
=
last
;
i
>=
0
;
i
--
)
{
current_value
=
fns
[
i
]
(
current_value
)
;
}
return
current_value
;
}
;
}
Insert cell
Insert cell
{
const
get_host
=
compose
(
get_value
,
search_host
,
get_env
)
;
// get_host is `_composed`
return
get_host
(
)
;
}
Insert cell
Insert cell
{
const
get_host
=
compose
(
test
,
ping
,
get_value
,
search_host
,
get_env
)
;
return
get_host
(
)
;
}
Insert cell
Insert cell
function
pipe
(
...
fns
)
{
return
function
_piped
(
...
args
)
{
// call the first function
// with the arguments of `_piped`
let
current_value
=
fns
[
0
]
(
...
args
)
;
// loop through the rest in the original order
for
(
let
i
=
1
;
i
<
fns
.
length
;
i
++
)
{
current_value
=
fns
[
i
]
(
current_value
)
;
}
return
current_value
;
}
;
}
Insert cell
Insert cell
{
const
get_host
=
pipe
(
get_env
,
search_host
,
get_value
)
;
return
get_host
(
)
;
}
Insert cell
Insert cell
Insert cell
function
cat
(
filepath
)
{
return
fs
.
readFileSync
(
filepath
,
'utf-8'
)
;
}
Insert cell
function
grep
(
pattern
,
content
)
{
const
exp
=
new
RegExp
(
pattern
)
;
const
lines
=
content
.
split
(
'\n'
)
;
return
lines
.
find
(
line
=>
exp
.
test
(
line
)
)
;
}
Insert cell
function
cut
(
{
delimiter
,
fields
}
,
str
)
{
return
str
.
split
(
delimiter
)
[
fields
-
1
]
;
}
Insert cell
Insert cell
cut
(
{
delimiter
:
'='
,
fields
:
2
}
,
grep
(
'^HOST='
,
cat
(
'.env'
)
)
)
;
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
const
get_host
=
pipe
(
cat
,
grep
.
bind
(
null
,
'^HOST='
)
,
cut
.
bind
(
null
,
{
delimiter
:
'='
,
fields
:
2
}
)
)
;
return
get_host
(
'.env'
)
;
}
Insert cell
Insert cell
{
const
get_host
=
pipe
(
cat
,
content
=>
grep
(
'^HOST='
,
content
)
,
str
=>
cut
(
{
delimiter
:
'='
,
fields
:
2
}
,
str
)
)
;
return
get_host
(
'.env'
)
;
}
Insert cell
Insert cell
Insert cell
Insert cell
{
const
add_one
=
num
=>
num
+
1
;
const
number
=
[
41
]
;
const
empty
=
[
]
;
return
{
number
:
number
.
map
(
add_one
)
.
join
(
', '
)
,
empty
:
empty
.
map
(
add_one
)
.
join
(
', '
)
}
;
}
Insert cell
Insert cell
Insert cell
Insert cell
{
function
cat
(
filepath
)
{
try
{
return
Result_v1
.
Ok
(
fs
.
readFileSync
(
filepath
,
'utf-8'
)
)
;
}
catch
(
e
)
{
return
Result_v1
.
Err
(
e
)
;
}
}
return
cat
(
'.env'
)
;
}
Insert cell
Insert cell
{
function
cat
(
filepath
)
{
try
{
return
Result_v1
.
Ok
(
fs
.
readFileSync
(
filepath
,
'utf-8'
)
)
;
}
catch
(
e
)
{
return
Result_v1
.
Err
(
e
)
;
}
}
return
cat
(
'.env'
)
.
map
(
grepp
(
'^HOST='
)
)
.
map
(
cutt
(
{
delimiter
:
'='
,
fields
:
2
}
)
)
;
}
Insert cell
Insert cell
Insert cell
Insert cell
{
const
safer_cat
=
Result_v2
.
make_safe
(
cat
)
;
return
safer_cat
(
'.env'
)
.
map
(
grepp
(
'^HOST='
)
)
.
map
(
cutt
(
{
delimiter
:
'='
,
fields
:
2
}
)
)
}
Insert cell
Insert cell
Insert cell
Insert cell
{
const
safer_cat
=
Result_v3
.
make_safe
(
cat
)
;
const
format_error
=
e
=>
`Whoops: ${
e
.
message
}`
;
return
safer_cat
(
'what?'
)
.
map
(
grepp
(
'^HOST='
)
)
.
map
(
cutt
(
{
delimiter
:
'='
,
fields
:
2
}
)
)
.
catchMap
(
format_error
)
;
}
Insert cell
Insert cell
Insert cell
Insert cell
{
const
constant
=
arg
=>
(
)
=>
arg
;
const
identity
=
arg
=>
arg
;
const
safer_cat
=
Result_v4
.
make_safe
(
cat
)
;
return
safer_cat
(
'what?'
)
.
map
(
grepp
(
'^HOST='
)
)
.
map
(
cutt
(
{
delimiter
:
'='
,
fields
:
2
}
)
)
.
cata
(
constant
(
"This ain't right"
)
,
identity
)
}
Insert cell
Insert cell
Insert cell
Insert cell
{
const
maybe_host
=
Maybe_v1
.
wrap_fun
(
grepp
(
'^HOST='
)
)
;
return
maybe_host
(
cat
(
'.env'
)
)
.
catchMap
(
(
)
=>
'Nothing()'
)
.
cata
(
identity
,
identity
)
;
}
Insert cell
Insert cell
safer_cat
(
'.env'
)
.
map
(
maybe_host
)
.
cata
(
identity
,
identity
)
;
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
maybe_host
=
Maybe_v2
.
wrap_fun
(
grepp
(
'^HOST='
)
)
;
Insert cell
safer_cat
(
'.env'
)
.
flatMap
(
maybe_host
)
.
catchMap
(
(
)
=>
'what?'
)
.
cata
(
identity
,
identity
)
;
Insert cell
Insert cell
{
const
safer_cat
=
Result_v5
.
make_safe
(
cat
)
;
const
maybe_host
=
Maybe_v2
.
wrap_fun
(
grepp
(
'^HOST='
)
)
;
const
get_value
=
Maybe_v2
.
wrap_fun
(
cutt
(
{
delimiter
:
'='
,
fields
:
2
}
)
)
;
const
host
=
safer_cat
(
'.env'
)
.
flatMap
(
maybe_host
)
.
flatMap
(
get_value
)
.
cata
(
constant
(
'http://127.0.0.1:3000'
)
,
identity
)
;
return
host
;
}
Insert cell
Insert cell
{
const
chain
=
fn
=>
m
=>
m
.
flatMap
(
fn
)
;
const
unwrap_or
=
fallback
=>
fm
=>
fm
.
cata
(
constant
(
fallback
)
,
identity
)
;
const
safer_cat
=
Result_v5
.
make_safe
(
cat
)
;
const
maybe_host
=
Maybe_v2
.
wrap_fun
(
grepp
(
'^HOST='
)
)
;
const
get_value
=
Maybe_v2
.
wrap_fun
(
cutt
(
{
delimiter
:
'='
,
fields
:
2
}
)
)
;
const
get_host
=
pipe
(
safer_cat
,
chain
(
maybe_host
)
,
chain
(
get_value
)
,
unwrap_or
(
'http://127.0.0.1:3000'
)
)
;
return
get_host
(
'.env'
)
;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Purpose-built for displays of data
Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Try it for free
Learn more
Fork
View
Export
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
numbers
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
repeat_twice
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
map
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
get_env
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
search_host
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
get_value
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
compose
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
pipe
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
cat
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
grep
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
cut
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
grepp
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
cutt
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Result_v1
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Result_v2
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Result_v3
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Result_v4
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Maybe_v1
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Result_v5
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Maybe_v2
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
maybe_host
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
safer_cat
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
constant
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
identity
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
fs
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
ping
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
test
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML