►
From YouTube: Functional programming guerilla in the land of Rust - Tomasz BaraĆski (Lambda Days 2017)
Description
http://www.lambdadays.org/lambdadays2017/tomasz-baranski
Rust is a systems programming language focused on safety, speed and concurrency. At the first glance Rust doesn't seem like a functional language. However, closer look reveals features like type inference, immutable data, traits that look a lot like type classes, algebraic data types, pattern matching.
During this presentation you will learn functional features of Rust and see some examples of how well-known functional patterns can be implemented in it. Prior knowledge of Rust is not required.
A
The
two
things
that
function
have
to
do
in
a
functional
programming,
language
and
I
love
those
nasty
tricks
with
being
first
class
and
pure.
How
do
I
search
is
that
a
first
class
function
is
a
function
that
could
be
created
on
demand
summon
in
code.
We
started
in
a
variable
passed
as
an
argument
to
function
a
return
from
from
a
function.
A
So
can
you
read
this
code?
Is
it
big
enough,
but
in
the
back
yeah?
Okay,
I
can
tie
you
closer.
So
this
is
very,
very
simple
function.
It
just
takes
it
up
an
argument
called
X
and
that's
5.
It
actually
close
another
function,
but
for
the
sake
of
this
talk,
this
will
do
Andi.
You
can
I
can
sew
it
in
in
a
variable
called
at
5.
A
Rust
doesn't
makes
me
type
all
type
annotations
unintended,
but
these
are
strong.
Typed
closes.
I
cannot
take
another
closer
and
I'll
pass
it
to
folks
to
a
function.
That
requires
a
closure,
a
function
with
two
arguments.
The
only
one,
and
probably
this
this
code
compares
to
integer
all
will
good
close
to
a
function
or
closure.
That
requires
an
integer
argument.
A
Naturally,
I
can
for
it
to
be
a
little
bit
integer
by
requiring
it
to
return
it
type.
Inference
in
Russ
is
on
par
with
what
Haskell
predators
can
do.
It
can
reduce
the
types
and
fill
all
those
two
NASA
details,
so
that
I
don't
have
to
worry
about
it,
so
yeah
I
got
a
to
the
function,
and
so
little
project
or
great,
so
I
can
write
a
function
that
returns
a
function.
A
It
creates
these
other
functions,
create
another
function
and
it
turns
it
on
it,
so
can
be
referenced
somewhere
else
and
actually
last
compiler
is
smart
enough
to
understand
that
this
is
function
which
will
decode
somewhere
and
I
totally
told
code
it's
on
on
the
heap,
but
depending
on
the
context,
I
use
it
in
it
probably
optimized
out
the
the
allocation.
So
this
will
cost
me
nothing
in
terms
of
performance
and
I'm
sure
there's
yesterday,
that
is
very
little
in
it
like
very
little
nice.
A
If
the
syntax
in
nightly
rest
that
just
don't
bother
with
box
things,
we
will
do
it
for
you
nicely
Russ,
Russ
tools,
channels
stable,
which
is
well
label.
It's
nicely
widget
for
experiments
and
things
that
need
to
sometimes
to
be
tested
on
different
contexts.
We
talked
before
they
become
stable,
so
yeah
I
got
the
function
that
returns
a
function
socket
can
have
a
function
that
takes
a
function
as
a
as
an
argument.
A
A
Haskell
is
a
great
example
of
of
languages.
Few
functions
and
thrusts
cannot
compare
to
that,
but
closes
is
said
that
they
have
functions
are
pure
the
things
they
are
operating
on
immutable
data
and
have
no
suggests
it's
not
true
in
closer,
but
in
general
they
do
not
do
nasty
things
to
the
data.
Don't
change
the
values
when
you're
not
looking
and
I
can
do
something.
That's
simple,
but
it's
actually
treating
you
can
define
pure
function
like
that
in
C,
as
well
so
late,
something
this
something
else.
A
A
struct
is
something
like
an
object
in
rust,
so
you
have
a
piece
of
memory
that
consists
of
two
integers
integers,
yes
and
have
something
that
operates
on
it,
and
messages
were
to
define
methods
in
last
and
looking
at
the
type
signature
of
this
function,
it
is
pretty
much
get
what
it's
supposed
to
do.
It
takes
self
and
the
number
another
number
and
retains
another
point
its
call
to
move
by,
so
you
can
still
do
not
guess
what
will
be
inside
the
function,
but
look
at
this
one.
A
It's
almost
the
same,
but
take
save
as
a
mute
or
multiple
reference.
That
means
this
function
is
supposed
to
change
the
object
itself,
the
object
it's
called
on
upon,
so
you
get
pretty
much
guess
what
this
should
be
doing
and,
of
course,
when
the
first
function
would
cannot
modify
self
is
to
return
something
here
like
this
is
a
new
point
with
those
fields
X
x
and
y
increased
by
respective
integers,
and
you
also
one
mutates
things
in
place
and.
A
If
you
ever
make
a
mistake
and
forget
them,
you
think
the
compiler
will
just
say
you
cannot
do
this
and
I
just
put
the
basic
and-
and
this
way
when
you
you,
you
have
something
some
some
piece
of
code
and
you're
looking
type
signatures.
You
just
see
that
things
that
can
change
and
see
the
things
that
guaranteed
not
to
change
ever.
This
is
not
true,
I
I
thought.
Well,
you
can
of
course
change
things
like
you,
can
instantly
print
statement
and
change.
A
The
code
word
around
your
code
and
you
can
do
all
kind
of
of
things
like
open
files,
open
connections,
stop
databases,
try
to
whatever
modify
some
some
some
memory,
and
this
isn't
change
the
values
past
the
function,
the
compiler
will
not
not
complain,
so
it's
not
pure
pure,
but
its
purest
law.
As
long
as
only
talking
about
the
parameters
of
the
function,
so
you
may
be
anxious.
A
Okay,
if
I
can
open
a
connection,
maybe
I
can
store
something
in
some
global
state
global
variable
and
introduce
some
some
kind
of
change
there,
and
you
can
certainly
do
that.
This
is
a
way
to
implement
a
static
global
variable
in
in
rust,
and
you
can
of
course
say
it's
immutable
variable
and
you
can
try
to
change
it,
but
Russell.
A
Are
you
really
sure
you
should
do
that
if
you
are,
if
this
does
not
a
mistake
market
so
Russ
would
require
to
surround
this
piece
of
code
is
safe
keyword,
which
means
that
this
is
a
way
to
take
compiler
to
not
worry
and
not
what
I'm
doing
trust
me.
This
will
be
all
right
and
the
body
will
enable
anybody.
A
Looking
at
your
code
later,
how
unsafe
I
should
be
should
be
worried,
worried
about
that
so
functions
in
Russ
are
pure
enough
for
all
practical
purposes,
and
because
it's
the
system,
ceramic
language
to
do
all
nasty
stuff
with
hardware
and
low-level
things
it
lets.
You
escape
those
pure
security
and
do
what
you
want.
Basically,.
A
Okay
functions,
see
his
functions
and
see
pretty
much
can
do
most
of
that
you
can
store
a
function.
You
can
pass
a
function.
You
can
create
them
in
runtime,
but
also
that
you
can
do
how
about
something
guys.
How
about
data
types
I've
mentioned
that
rust
is
a
strongly
typed
language.
There's
time
influence.
A
So
we
expect
that
a
modern
type
system
that
allows
do
very
nice
things
like
intimate
algebraic
data
types,
so
examples
in
this
section
will
be
taken
basically
from
learning
as
Haskell
and
translate
it
to
rust.
So
this
is
the
simplest
of
the
predator
pipe
you
can
eat.
You
can
have
it's
an
enum
just
like
in
C,
which
has
two
values,
and
you
can
use
them
in
variable,
but
this
is
again
treating
races
and
the
language
can
do
that.
Most
anglers
can
do
that.
It
is
something
more
complicated.
A
If
you
have
in
scope
something
else
which
uses
source
of
the
name
circle,
you
will
be
required
to
use
the
qualifier
shaped
column
column
to
to
integrate
it
so
yeah.
This
is
how
you
use
it
in
a
real
code.
You
it's
not
as
elegant
as
in
Haskell
or
Scala,
but
you
can
match
against
the
value
and
its
enemy
must
because
of
the
match.
Must
exhaust,
if
you
forget
something
compiler,
we
say
that
this
will
not
do,
and
you
have
all
those
nasty
all
those
nice
sorry
things
in
II.
A
But
this
one
thing
having
things
like
circles
and
rectangles
and
maybe
other
shapes,
can
we
have
something
more
generic,
a
type
that
can
hold
any
type
like
that,
like
maybe
in
Haskell,
just
hold
any
any
kind
of
value
and
sure
enough
last
have
an
option
type
which
is
so-called
generic
type.
It
can
hold
nothing
or
none
and
some
value
of
the
shape
of
the
T
and
you
can
use
it
and
the
type
inference
and
the
compiler
will
know
which
types
that
you
are.
A
Is
it
option
of
strings
option
of
in
anything
that
can
be
expressed
in
that
system
can
do
Butler
to
even
sometimes
we'd
like
to
have
some
restriction
on
the
type
like
this
is
not
a
enemy
destruct
again,
which
has
three
type
variables
each
of
the
synthesis
of
this
one
type,
and
we
can
just
create
it
in
line
just
putting
values.
So
the
constructor
of
the
of
the
object
destruct
and
have
last
figure
out
what
to
do
with
that.
A
A
Case
inverse
look
a
little
like
type
classes
in
other
languages,
a
trait
quoting
Wrath,
diverse
book.
This
is
only
one
at
the
moment.
It's
a
trade
trade
is
a
language
feature
that
tells
the
last
compiler
about
functionality
at
times
must
provide
on
a
type
class,
the
sort
of
interface
that
defines
some
behavior.
A
You
can
say
that
it's
pretty
much
the
same
thing
and
let's
see
how
it
works
in
Haskell.
Of
course,
you
have
some
color,
which
will
take
one
of
three
values,
create
some
binding
for
them
and
try
to
compare
those,
as
these
are
the
same
if
it
works
you
need
to.
You
need
to
have
the
your
type
implements
the
EQ
type
class,
which
defines
two
operators
equals
and
not
equals
and
provides
default
implementations
for
those.
So
when
you
implement
them,
you
can't
write
any
for
it
to
work,
and
this
parameter
is
welcome.
Rust.
A
This
same
coach
translate
to
us.
Give
an
enum
which
had
sin
half
can
take
one
of
three
values.
We've
been:
do
you
create
variables?
You
come
them
and
again
to
have
it
to
be
able
to
do
you
to
the
operation
into
demanded
trade.
Goes
surprisingly
enough,
EQ,
which
provides
two
functions
which
have
default
implementation?
This
is
not
the
real
EQ
class
from
Russ.
It
is
a
little
most
complicated,
but
I.
A
This
works
exactly
the
same
as
in
rust,
and
it
can
be
done
rust
and
trace.
I
used
to
do
all
those
funny
things
and
like
like.
Oh
sorry,
you
need
to
evil
to
might
want
to
implement
this,
but
this
is
silly
nobody.
Nobody
would
do
that
for
this
simple
thing,
so
you
can
have
it
automatically
derived
this
what's
compiled.
Is
that
for
you,
you
know
how
to
do
deal
with
EQ
and
you
guys
it
for
you.
A
So
Restless
trades
is
very
important.
This
part
of
language
it
uses
quality
which
defines
two
to
three.
It's
partially
positive
equality
ordering
comparing
to
two
values
to
produce
an
order,
also
you've
done
with
traits
clone
and
copy
clone,
and
they
do
basically
the
same,
but
the
distinction
between
them
is
coast,
copies
the
rise
of
them
automatically
for
you.
If
the
type
is
simply
enough-
and
it's
always
used
inclusively,
never
use,
it
doesn't
write
any
methods.
A
Actually,
you
can
call
them
method
from
from
copy
and,
for
example,
individual
types
have
copy
implemented,
so
you
can
just
copy
integers
everybody
consider
the
cheap
operation
clone
and
enhanced
of
the
same,
but
is
considered
expensive.
So
when
you
implement
clone
for
your
type,
you
need
to
explicitly
call
clone
methods
and
it's
considered
expensive
operator
overloading
is
done
with
straight
add
a
design,
sub,
double
sign,
and
so
on
couple.
Dozens
of
them
accelerating
cover
types
is
also
implemented
in
terms
of
a
trade.
A
It's
a
trade
having
it
an
object
is
collabo
as
all
trades,
Jolly,
three
trades,
because
Russ
control
control,
suitability
looking
into
an
object
indexing
into
an
object
again,
a
trade
default
trade
which
basically
tells
compiler
how
to
produce
default
value
for
o
types
like
zero
for
integers
or
or
something
like
that
drop
is
used
to
control.
What
happens
when
your
value
goes
out
to
scramble
and
the
memory
gets
freed,
so
you
can
overall
implement
this
trade
just
an
extra
behavior
at
this
moment
sentencing.
A
Those
two
are
interesting
because
they're
using
thread
sent
items,
although
those
items
can
be
transferred
between
threads
safely,
so
they
visited
from
words
rated
the
trade.
The
thread
cannot
reference
them
anymore
and
then
appear
in
the
another
thread,
and
you
can
use
them
the
sync
items.
The
types
that
value
the
types
that
implement
things
rate
trade
can
be
shared
between
threads.
All
the
threads
in
the
program
can
now
use
them.
This
is
how
this
one
of
the
ways
that
makes
last
thread
safe,
reinsert,
safe
and
starting
with
last
1.15,
which
was
released
two
weeks
ago.
A
I
think
you
can
do
custom
device,
usually
compilers
know
how
to
derive
those
common
traits
like
EQ,
art
and
so
on.
You
can
write
some
procedural
macros
to
implement
and
there
is
a
trace.
You
desire
this
little
advance
last
black
box,
black
magic
last
and
I'm
not
going
to
this,
but
this
is
very,
very
nice
feature
that
you
use
in
couple
of
projects
actually
that
these
are
which
is
ORM
in
rust
or
cellulite
or
library,
which
brings
me
to
macros.
A
A
Like
all
my
cousin,
you
need
to
end
with
a
it's
amazing
point
is
how
you
mark
the
macros
into
compiler,
expects
back
it's
very
nice
syntax
to
create
lists
without
literals.
Try
is
meant
for
family
carers,
panic
about
your
program,
principles
well,
Prince
things,
but
also
macros
allows
you
to
build
pieces
of
syntax.
The
drugs
are
in
support
out
of
the
box
and.
B
A
Use
it
as
in
supposed
to
build
a
functional
composition
with
Argos
that
doesn't
support
compositional
fractions
out
of
the
box,
but
you
can
do
it.
Let's
say
we
have
specified
that
we
have
a
list
of
numbers,
a
vector
of
numbers
and
we
try
to
map
over
it
with
some
modern
look
or
less
trivial
example,
but
such
more
than
a
single
function,
and
we
can
use
that.
But
the
only
thing
I
care
about
in
those
yellow
line
line
is
the
ABS
function
and
in
negative
function.
A
This
is
the
Salomon
is
just
syntax
avenge,
don't
really
like
it.
I
would
I
would
like
not
to
use
it
anymore
ever
so,
let's
lie
down
as
a
micro
macro.
This
is
a
syntax
to
create
macros.
It
looks
a
lot
like
function.
Let's
thing
on
the
Left,
it
defines
a
pattern
that
the
compiler
will
look
for
and
the
thing
on
the
right
after
the
double
arrow
that
it
will
be
replaced
with
this.
So
in
this
case
we've
got
two
identifiers
dollar
F
ontology
supported
gadot,
and
this
will
this
will
be
translated
into
a
closure
function.
A
This
is
how
we
use
it
notice
that
I
didn't
use
the
Commission
point
in
the
definition
of
macros,
but
I
need
to
use
it
when
I
call
the
macro.
This
is
how
rust
works,
I
just
use
sanitation,
absolute
and
just
apply
it
and,
of
course,
worse
provides
you
with
a
method
to
expand
the
macro
to
see
what
is
actually
see
what
does
not
descend
and
sure
enough.
This
is
what
we
expected.
A
This
macro
has
problems
like.
There's
only
only
allows
supplying
methods
not
functions.
It's
only
I
was
applying
two
methods,
not
any
number
of
them,
which
is
good,
limiting
like
if
I
want
to
to
apply
the
function
like
that,
that's
right,
another
macro
macro:
let's
call
it
C
by
compose-
and
this
is
there
is
basic
stuff.
You
apply.
Those
functions
in
all
that
is
like
mathematical
definition
of
of
composing
functions,.
A
Listen
like
that
and
sure
enough.
It
expands
to
something,
and
we
expected
the
i30
to
column.
:
ids
is
a
so-called
Universal
function,
calls
and
ducts.
This
is
ways
first
or
is
a
way
to
uniformly
call
functions
and
methods
all
other
things
that
can
be
callable
so
that
we
can
unify
it
and
use
it
in
context
like
this,
and
since
we
can
rewrite
our
our
previous
example
with
this
new
macro
look
is
that
abs
is
a
function
from
the
type
and
energy
neck
is
a
function
from
the
trait
has
the
difference
in
casing
all
right.
A
So
let's
try
now
the
final
function.
That
applies
three
things.
Three
functions
you
can
be
tempted
to
do
like
this.
Just
try
them
in
order,
but
is
it
out
to
nowhere
right?
This
only
works
for
three
functions,
but
you
will
immediately
have
a
case
of
four
functions
and
five
functions,
etc
and
rest
provides
macros
absolute
classes.
You
can
try
to
implement
this
in
the
smarter
way
to
allow
na
and
number
of
functions,
and
is
it.
The
first
line
is
as
before.
When
we
have
just
two
functions,
we
know
what
to
do.
A
The
the
blue
things
lost
the
most.
The
first
function,
sum
from
the
list
is
applied
to
the
result
of
calling
the
green
function
to
the
purple
argument,
and
the
green
function
is
application
of
this
macro
for
two
functions,
and
we
can
go
crazy
now
we
can
go
crazy
with
this
macro
can
do
anything
we
want.
A
This
is
thinking
these
numbers
filtering
to
get
the
vertex
absolute
values
of
those
numbers.
Then
we
negate
them.
Then
we
count
once
in
binary
representation.
Then
we
cast
it
to
cyan't
integer
because
count
one
citizen
a
sign
and
sandy
digital,
and
then
we
apply
an
even
function
actually
now,
if
we
can
see
what
it
expands
to
end,
this
is
some
some
horrible
code
which
has
great
for
closures
applies
them
create
them
applies
them.
It's
not
something.
A
human
of
right
would
write
right.
A
So
yeah
you
can
implement
all
those
nice
features
like
functional
composition,
use
it
anywhere.
You
go
back.
You
can
see
that
there
are
methods.
There
are
trace
methods
there
are
functions,
they
are,
is
an
even
closer
defined
in
on
the
spot.
So
can
you
do
anything
and
you're
not
when
you're
not
paying
for
it.
A
This
is
pretty
much
all
I
had
rust.
Easily
functions
provide
first
class
functions,
a
direct
language
that
provides
physical
functions,
which
are
pure
enough
for
practical,
practical
applications
that
languages
provides
algebraic
data
types.
We
can
do
all
nasty
things
with
that
have
trays
which
work
with
the
much
the
same
as
type
classes,
and
you
can
use
macros
to
create
all
those
things
that
are
missing
from
the
language.
B
You've
shown
in
your
example
of
macros
how
you
are
making
a
macro
over
working
over
identifiers,
but
it
is
possible
to
make,
for
example,
macro
which
could
be
applied
for
type
definitions.
For
example,
I
have
macro
that
takes
type
definition
as
a
parameter,
so
I
can,
for
example,
iterate
over
it
skills
or
fun.
Yes,
there.
A
You
see
there
as
the
the
FG
and
H
are
to
fix
this
column,
export
this
expression
and
the
first
macro
we
used
items
identifier
and
rust
provides
many
more
of
those
and
types
is,
one
of
them
is
py,
I.
Think
I,
remember
correctly,
which
would
make
the
compiler
expect
type
path
to
to
the
macro.
However,
it's
not
the
disease
as
it
as
it
is
here.
This
is
so
called
simple
macros
and
they
just
replacing
some
code
with
some
code
to
iterate
over
like
types
fields
or
enim
fields.