Platform
Solutions
Resources
Pricing
Sign in
Sign up
Jasper
Workspace
Fork
Public
By
Jasper
Edited
Oct 4, 2019
2 stars
Insert cell
Insert cell
data
=
[
1
,
2
,
3
,
4
,
5
]
Insert cell
Insert cell
data
.
map
(
x
=>
x
+
1
)
;
Insert cell
Insert cell
data
.
filter
(
x
=>
x
>
2
)
;
Insert cell
Insert cell
data
.
map
(
x
=>
x
+
1
)
.
filter
(
x
=>
x
>
2
)
;
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
Insert cell
Insert cell
reduce
=
(
reducer
,
init
,
coll
)
=>
{
reducer
=
buildReducer
(
reducer
)
;
if
(
coll
===
undefined
)
{
coll
=
init
;
init
=
reducer
.
init
(
)
;
}
if
(
init
===
undefined
)
{
init
=
reducer
.
init
(
)
;
}
let
result
=
init
;
for
(
let
x
of
coll
)
{
result
=
reducer
.
step
(
result
,
x
)
;
}
return
reducer
.
result
(
result
)
;
}
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
Insert cell
addMapToReducer
=
(
fn
,
reducer
)
=>
{
return
(
xs
,
x
)
=>
reducer
(
xs
,
fn
(
x
)
)
;
}
Insert cell
plusOneToArray
=
addMapToReducer
(
x
=>
x
+
1
,
arrayConcatReducer
)
Insert cell
Insert cell
reduce
(
plusOneToArray
,
[
]
,
data
)
Insert cell
reduce
(
addMapToReducer
(
x
=>
x
+
1
,
sumReducer
)
,
0
,
data
)
Insert cell
md
`### Transducer`
Insert cell
Insert cell
mapping
=
mapFn
=>
reducer
=>
{
return
(
xs
,
x
)
=>
reducer
(
xs
,
mapFn
(
x
)
)
;
}
Insert cell
mapping
(
x
=>
x
+
1
)
(
arrayConcatReducer
)
(
[
1
,
2
]
,
5
)
Insert cell
Insert cell
addOneTransducer
=
mapping
(
x
=>
x
+
1
)
Insert cell
reduce
(
addOneTransducer
(
sumReducer
)
,
5
,
data
)
Insert cell
reduce
(
addOneTransducer
(
arrayConcatReducer
)
,
[
]
,
data
)
Insert cell
reduce
(
addOneTransducer
(
objRandomKeyReducer
)
,
{
}
,
new
Set
(
[
55
,
56
]
)
)
Insert cell
Insert cell
filtering
=
pred
=>
reducer
=>
{
return
(
xs
,
x
)
=>
pred
(
x
)
?
reducer
(
xs
,
x
)
:
xs
}
Insert cell
gtTwoTransducer
=
filtering
(
x
=>
x
>
2
)
Insert cell
reduce
(
gtTwoTransducer
(
arrayConcatReducer
)
,
[
]
,
data
)
Insert cell
Insert cell
reduce
(
gtTwoTransducer
(
sumReducer
)
,
0
,
range
(
5
)
)
Insert cell
Insert cell
transduce
=
(
transducer
,
reducer
,
init
,
coll
)
=>
{
reducer
=
transducer
(
reducer
)
;
return
reduce
(
reducer
,
init
,
coll
)
}
Insert cell
transduce
(
addOneTransducer
,
sumReducer
,
0
,
data
)
Insert cell
Insert cell
Insert cell
{
let
reducer
=
filtering
(
x
=>
x
>
2
)
(
mapping
(
x
=>
x
+
1
)
(
arrayConcatReducer
)
)
;
return
reduce
(
reducer
,
[
]
,
[
1
,
2
,
3
,
4
,
5
]
)
}
Insert cell
Insert cell
Insert cell
compose2
=
(
f
,
g
)
=>
(
...
args
)
=>
f
(
g
(
...
args
)
)
Insert cell
compose
=
(
...
fns
)
=>
reduce
(
compose2
,
fns
.
shift
(
)
,
fns
)
;
Insert cell
Insert cell
{
let
reducer
=
compose
(
filtering
(
x
=>
x
>
2
)
,
mapping
(
x
=>
x
+
1
)
)
(
arrayConcatReducer
)
return
reduce
(
reducer
,
[
]
,
[
1
,
2
,
3
,
4
,
5
]
)
}
Insert cell
transduce
(
compose
(
filtering
(
x
=>
x
>
2
)
,
mapping
(
x
=>
x
+
1
)
)
,
arrayConcatReducer
,
[
]
,
[
1
,
2
,
3
,
4
,
5
]
)
Insert cell
Insert cell
{
function
filterThenMapThenToArrayReducer
(
xs
,
x
)
{
if
(
x
>
2
)
{
x
=
x
+
1
;
xs
.
push
(
x
)
;
return
xs
;
}
else
{
return
xs
;
}
}
return
reduce
(
filterThenMapThenToArrayReducer
,
[
]
,
[
1
,
2
,
3
,
4
,
5
]
)
}
Insert cell
Insert cell
flatten
=
reducer
=>
(
xs
,
x
)
=>
reduce
(
reducer
,
xs
,
x
)
Insert cell
flatMap
=
fn
=>
{
return
compose
(
mapping
(
fn
)
,
flatten
)
}
Insert cell
bufferCount
=
bufferSize
=>
reducer
=>
{
let
n
=
0
,
els
=
[
]
;
return
(
xs
,
x
)
=>
{
els
[
n
++
]
=
x
;
if
(
n
===
bufferSize
)
{
const
result
=
els
;
els
=
[
]
;
n
=
0
;
return
reducer
(
xs
,
result
)
;
}
else
{
return
xs
}
}
}
Insert cell
reduce
(
bufferCount
(
3
)
(
arrayConcatReducer
)
,
[
]
,
[
1
,
2
,
3
,
4
,
5
,
6
]
)
Insert cell
{
const
string
=
"I *!#* love you"
;
const
transducer
=
compose
(
flatMap
(
str
=>
str
.
split
(
' '
)
)
,
filtering
(
x
=>
x
!==
"*!#*"
)
,
mapping
(
x
=>
x
===
"you"
?
x
+
"tube"
:
x
)
)
;
return
transduce
(
transducer
,
arrayConcatReducer
,
[
]
,
[
string
]
)
.
join
(
' '
)
;
}
Insert cell
transduce
(
compose
(
mapping
(
x
=>
x
*
2
)
,
filtering
(
x
=>
x
>
5
)
,
mapping
(
x
=>
x
+
1
)
)
,
sumReducer
,
0
,
[
1
,
2
,
3
,
4
,
5
]
)
Insert cell
dropping
=
n
=>
reducer
=>
{
return
(
xs
,
x
)
=>
{
let
toDrop
=
n
;
if
(
toDrop
===
0
)
return
reducer
(
xs
,
x
)
else
toDrop
--
;
}
}
Insert cell
t1
=
filtering
(
x
=>
x
>
2
)
Insert cell
t2
=
filtering
(
x
=>
x
>
3
)
Insert cell
t
=
mapping
(
x
=>
x
+
1
)
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.
Insert cell
splitting
=
(
transducers
)
=>
(
reducers
)
=>
{
const
keys
=
Object
.
keys
(
transducers
)
;
const
reds
=
keys
.
reduce
(
(
obj
,
k
)
=>
(
obj
[
k
]
=
transducers
[
k
]
(
reducers
[
k
]
)
,
obj
)
,
{
}
)
;
const
xss
=
{
}
;
return
(
xs
,
x
)
=>
{
return
keys
.
reduce
(
(
obj
,
k
)
=>
(
obj
[
k
]
=
reds
[
k
]
(
xs
[
k
]
,
x
)
,
obj
)
,
{
}
)
}
}
Insert cell
{
return
transduce
(
compose
(
t
,
splitting
(
{
foo
:
t1
,
bar
:
t2
}
)
)
,
{
foo
:
arrayConcatReducer
,
bar
:
arrayConcatReducer
}
,
{
foo
:
[
]
,
bar
:
[
]
}
,
[
1
,
2
,
3
,
4
,
5
]
)
}
Insert cell
One platform
to build and deploy the best data apps
Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Try it for free
Learn more
Fork
View
Export
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
data
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
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
map
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
fromMap
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
fromObj
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
filter
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
filteredFromArray
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
filteredFromSet
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
reduce
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
arrayConcatReducer
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
objRandomKeyReducer
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
strTalkReducer
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
buildReducer
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
sumReducer
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
ArrayToArray
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
StringToObject
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
SetToString
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
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
addMapToReducer
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
plusOneToArray
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
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
mapping
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
addOneTransducer
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
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
filtering
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
gtTwoTransducer
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
range
Edit
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
transduce
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
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
compose2
Add comment
Copy import
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
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
flatten
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
flatMap
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
bufferCount
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
dropping
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
t1
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
t2
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
t
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
splitting
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML