Skip to content

Commit

Permalink
update appendixes with all the code present in support
Browse files Browse the repository at this point in the history
  • Loading branch information
KtorZ committed Jan 8, 2018
1 parent 6739f6a commit 08059eb
Show file tree
Hide file tree
Showing 8 changed files with 543 additions and 109 deletions.
44 changes: 36 additions & 8 deletions SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,46 @@
* [In Summary](ch12.md#in-summary)
* [Exercises](ch12.md#exercises)
* [Appendix A: Essential Functions Support](appendix_a.md)
* [chain](appendix_a.md#chain)
* [always](appendix_a.md#always)
* [compose](appendix_a.md#compose)
* [curry](appendix_a.md#curry)
* [either](appendix_a.md#either)
* [identity](appendix_a.md#identity)
* [inspect](appendix_a.md#inspect)
* [join](appendix_a.md#join)
* [liftA2](appendix_a.md#liftA2)
* [liftA3](appendix_a.md#liftA3)
* [left](appendix_a.md#left)
* [liftA\*](appendix_a.md#lifta)
* [maybe](appendix_a.md#maybe)
* [nothing](appendix_a.md#maybe)
* [reject](appendix_a.md#reject)
* [Appendix B: Algebraic Structures Support](appendix_b.md)
* [Either](appendix_b.md#Either)
* [Identity](appendix_b.md#Identity)
* [IO](appendix_b.md#IO)
* [Maybe](appendix_b.md#Maybe)
* [Compose](appendix_b.md#compose)
* [Either](appendix_b.md#either)
* [Identity](appendix_b.md#identity)
* [IO](appendix_b.md#io)
* [List](appendix_b.md#list)
* [Map](appendix_b.md#map)
* [Maybe](appendix_b.md#maybe)
* [Task](appendix_b.md#task)
* [Appendix C: Pointfree Utilities](appendix_c.md)
* [chain](appendix_c.md#chain)
* [concat](appendix_c.md#concat)
* [filter](appendix_c.md#filter)
* [flip](appendix_c.md#flip)
* [forEach](appendix_c.md#foreach)
* [head](appendix_c.md#head)
* [intercalate](appendix_c.md#intercalate)
* [join](appendix_c.md#join)
* [last](appendix_c.md#last)
* [map](appendix_c.md#map)
* [match](appendix_c.md#match)
* [prop](appendix_c.md#prop)
* [reduce](appendix_c.md#reduce)
* [safeHead](appendix_c.md#safehead)
* [safeLast](appendix_c.md#safelast)
* [safeProp](appendix_c.md#safeprop)
* [sequence](appendix_c.md#sequence)
* [sortBy](appendix_c.md#sortby)
* [split](appendix_c.md#split)
* [take](appendix_c.md#take)
* [traverse](appendix_c.md#traverse)
* [unsafePerformIO](appendix_c.md#unsafeperformio)
101 changes: 57 additions & 44 deletions appendix_a.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Appendix A: Functions Support
# Appendix A: Essential Functions Support

In this appendix, you'll find some basic JavaScript implementations of various functions
described in the book. Keep in mind that these implementations may not be the fastest or the
Expand All @@ -9,23 +9,18 @@ In order to find functions that are more production-ready, have a peak at

Note that some functions also refer to algebraic structures defined in the [Appendix B](./appendix_b.md)

## chain

```hs
chain :: Monad m => (a -> m b) -> m a -> m b
```
## always

```js
const chain = f => compose(join, map(f))
// always :: a -> b -> a
const always = curry((a, b) => a);
```

## compose

```hs
compose :: ((a -> b), (b -> c), ..., (y -> z)) -> a -> z
```
## compose

```js
// compose :: ((a -> b), (b -> c), ..., (y -> z)) -> a -> z
function compose(...fns) {
const n = fns.length;

Expand All @@ -41,13 +36,11 @@ function compose(...fns) {
}
```

## curry

```hs
curry :: ((a, b, ...) -> c) -> a -> b -> ... -> c
```
## curry

```js
// curry :: ((a, b, ...) -> c) -> a -> b -> ... -> c
function curry(fn) {
const arity = fn.length;

Expand All @@ -61,13 +54,11 @@ function curry(fn) {
}
```

## either

```hs
either :: (a -> c) -> (b -> c) -> Either a b -> c
```
## either

```js
// either :: (a -> c) -> (b -> c) -> Either a b -> c
const either = curry((f, g, e) => {
if (e.isLeft) {
return f(e.$value);
Expand All @@ -77,13 +68,19 @@ const either = curry((f, g, e) => {
});
```

## inspect

```hs
inspect :: a -> String
## identity

```js
// identity :: x -> x
const identity = x => x;
```


## inspect

```js
// inspect :: a -> String
function inspect(x) {
if (x && typeof x.inspect === 'function') {
return x.inspect();
Expand All @@ -93,52 +90,53 @@ function inspect(x) {
return f.name ? f.name : f.toString();
}

function inspectTerm(t) {
switch (typeof t) {
case 'string':
return `'${t}'`;
case 'object': {
const ts = Object.keys(t).map(k => [k, inspect(t[k])]);
return `{${ts.map(kv => kv.join(': ')).join(', ')}}`;
}
default:
return String(t);
}
}

function inspectArgs(args) {
const str = args.reduce((acc, x) => `${acc}, ${inspect(x)}`, '');
return `(${str})`;
return Array.isArray(args) ? `[${args.map(inspect).join(', ')}]` : inspectTerm(args);
}

return (typeof x === 'function') ? inspectFn(x) : inspectArgs(x);
}
```

## join

```hs
join :: Monad m => m (m a) -> m a
```
## left

```js
const join = m => m.join();
// left :: a -> Either a b
const left = a => new Left(a);
```

## liftA2

```hs
liftA2 :: (Applicative f) => (a1 -> a2 -> b) -> f a1 -> f a2 -> f b
```
## liftA\*

```js
// liftA2 :: (Applicative f) => (a1 -> a2 -> b) -> f a1 -> f a2 -> f b
const liftA2 = curry((fn, a1, a2) => a1.map(fn).ap(a2));
```

## liftA3

```hs
liftA3 :: (Applicative f) => (a1 -> a2 -> a3 -> b) -> f a1 -> f a2 -> f a3 -> f b
```

```js
const liftA3 = curry((fn, a1, a2, a3) => a1.map(fn).ap(a2).ap(a3))
// liftA3 :: (Applicative f) => (a1 -> a2 -> a3 -> b) -> f a1 -> f a2 -> f a3 -> f b
const liftA3 = curry((fn, a1, a2, a3) => a1.map(fn).ap(a2).ap(a3));
```

## maybe

```hs
maybe :: b -> (a -> b) -> Maybe a -> b
```
## maybe

```js
// maybe :: b -> (a -> b) -> Maybe a -> b
const maybe = curry((v, f, m) => {
if (m.isNothing) {
return v;
Expand All @@ -147,3 +145,18 @@ const maybe = curry((v, f, m) => {
return f(m.$value);
});
```


## nothing

```js
// nothing :: () -> Maybe a
const nothing = () => Maybe.of(null);
```

## reject

```js
// reject :: a -> Task a b
const reject = a => Task.rejected(a);
```
Loading

0 comments on commit 08059eb

Please sign in to comment.