GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — functional-programming-erlang/week3.erl at master · esturcke/functional-programming-erlang

Expand file tree

/

week3.erl

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
36 lines (26 loc) · 1.22 KB
/

week3.erl

Copy path

File metadata and controls

36 lines (26 loc) · 1.22 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
-module(week3).
-export([doubleAll/1, evens/1, product/1, zip/2, zip_with/3, zip_with_alt/3, zip_alt/2, compose/1, twice/1, iterate/1]).
-spec doubleAll([number()]) -> [number()].
doubleAll(Xs) -> lists:map(fun(X) -> X * 2 end, Xs).
-spec evens([integer()]) -> [integer()].
evens(Xs) -> lists:filter(fun(X) -> X rem 2 == 0 end, Xs).
-spec product([number()]) -> number().
product(Xs) -> lists:foldr(fun(A, B) -> A * B end, 1, Xs).
-spec zip([A], [B]) -> [{A, B}].
zip([], _) -> [];
zip(_, []) -> [];
zip([X|Xs], [Y|Ys]) -> [{X, Y}|zip(Xs, Ys)].
-spec zip_with(fun((X, Y) -> T), [X], [Y]) -> [T].
zip_with(_, [], _) -> [];
zip_with(_, _, []) -> [];
zip_with(F, [X|Xs], [Y|Ys]) -> [F(X, Y)|zip_with(F, Xs, Ys)].
-spec zip_with_alt(fun((X, Y) -> T), [X], [Y]) -> [T].
zip_with_alt(F, Xs, Ys) -> lists:map(fun({X, Y}) -> F(X, Y) end, zip(Xs, Ys)).
-spec zip_alt([A], [B]) -> [{A, B}].
zip_alt(Xs, Ys) -> zip_with(fun(X, Y) -> {X, Y} end, Xs, Ys).
-spec compose([fun()]) -> fun().
compose(Fs) -> fun(X) -> lists:foldr(fun(F, Acc) -> F(Acc) end, X, Fs) end.
-spec twice(fun()) -> fun().
twice(F) -> compose([F, F]).
-spec iterate(integer()) -> fun((fun((X) -> X)) -> fun((X) -> X)).
iterate(N) -> fun(F) -> compose(lists:duplicate(N, F)) end.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.