# Lampiran C: Utilitas Pointfree

Dalam lampiran ini, Anda akan menemukan beberapa implementasi JavaScript dasar dari berbagai fungsi yang dijelaskan dalam buku ini.

Ingatlah bahwa implementasi ini mungkin bukan implementasi tercepat atau paling efisien di luar sana; mereka dibuat *hanya untuk tujuan pendidikan*.

Untuk menemukan fungsi yang lebih siap produksi, lihat [ramda](https://ramdajs.com/), [lodash](https://lodash.com/), atau [folktale](http://folktale.origamitower.com/).

Perhatikan bahwa fungsi mengacu pada `curry` & `compose` fungsi yang didefinisikan dalam [Lampiran A](/belajar-functional-programming-javascript/appendix_a-id.md).

## add

```javascript
// add :: Number -> Number -> Number
const add = curry((a, b) => a + b);
```

## append

```javascript
// append :: String -> String -> String
const append = flip(concat);
```

## chain

```javascript
// chain :: Monad m => (a -> m b) -> m a -> m b
const chain = curry((fn, m) => m.chain(fn));
```

## concat

```javascript
// concat :: String -> String -> String
const concat = curry((a, b) => a.concat(b));
```

## eq

```javascript
// eq :: Eq a => a -> a -> Boolean
const eq = curry((a, b) => a === b);
```

## filter

```javascript
// filter :: (a -> Boolean) -> [a] -> [a]
const filter = curry((fn, xs) => xs.filter(fn));
```

## flip

```javascript
// flip :: (a -> b -> c) -> b -> a -> c
const flip = curry((fn, a, b) => fn(b, a));
```

## forEach

```javascript
// forEach :: (a -> ()) -> [a] -> ()
const forEach = curry((fn, xs) => xs.forEach(fn));
```

## head

```javascript
// head :: [a] -> a
const head = (xs) => xs[0];
```

## intercalate

```javascript
// intercalate :: String -> [String] -> String
const intercalate = curry((str, xs) => xs.join(str));
```

## join

```javascript
// join :: Monad m => m (m a) -> m a
const join = (m) => m.join();
```

## last

```javascript
// last :: [a] -> a
const last = (xs) => xs[xs.length - 1];
```

## map

```javascript
// map :: Functor f => (a -> b) -> f a -> f b
const map = curry((fn, f) => f.map(fn));
```

## match

```javascript
// match :: RegExp -> String -> Boolean
const match = curry((re, str) => re.test(str));
```

## prop

```javascript
// prop :: String -> Object -> a
const prop = curry((p, obj) => obj[p]);
```

## reduce

```javascript
// reduce :: (b -> a -> b) -> b -> [a] -> b
const reduce = curry((fn, zero, xs) => xs.reduce(fn, zero));
```

## replace

```javascript
// replace :: RegExp -> String -> String -> String
const replace = curry((re, rpl, str) => str.replace(re, rpl));
```

## reverse

```javascript
// reverse :: [a] -> [a]
const reverse = (x) =>
  Array.isArray(x) ? x.reverse() : x.split("").reverse().join("");
```

## safeHead

```javascript
// safeHead :: [a] -> Maybe a
const safeHead = compose(Maybe.of, head);
```

## safeLast

```javascript
// safeLast :: [a] -> Maybe a
const safeLast = compose(Maybe.of, last);
```

## safeProp

```javascript
// safeProp :: String -> Object -> Maybe a
const safeProp = curry((p, obj) => compose(Maybe.of, prop(p))(obj));
```

## sequence

```javascript
// sequence :: (Applicative f, Traversable t) => (a -> f a) -> t (f a) -> f (t a)
const sequence = curry((of, f) => f.sequence(of));
```

## sortBy

```javascript
// sortBy :: Ord b => (a -> b) -> [a] -> [a]
const sortBy = curry((fn, xs) =>
  xs.sort((a, b) => {
    if (fn(a) === fn(b)) {
      return 0;
    }

    return fn(a) > fn(b) ? 1 : -1;
  })
);
```

## split

```javascript
// split :: String -> String -> [String]
const split = curry((sep, str) => str.split(sep));
```

## take

```javascript
// take :: Number -> [a] -> [a]
const take = curry((n, xs) => xs.slice(0, n));
```

## toLowerCase

```javascript
// toLowerCase :: String -> String
const toLowerCase = (s) => s.toLowerCase();
```

## toString

```javascript
// toString :: a -> String
const toString = String;
```

## toUpperCase

```javascript
// toUpperCase :: String -> String
const toUpperCase = (s) => s.toUpperCase();
```

## traverse

```javascript
// traverse :: (Applicative f, Traversable t) => (a -> f a) -> (a -> f b) -> t a -> f (t b)
const traverse = curry((of, fn, f) => f.traverse(of, fn));
```

## unsafePerformIO

```javascript
// unsafePerformIO :: IO a -> a
const unsafePerformIO = (io) => io.unsafePerformIO();
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://renomureza.gitbook.io/belajar-functional-programming-javascript/appendix_c-id.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
