Skip to content

Commit

Permalink
Added: doc site
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-lee committed Nov 7, 2020
1 parent 5e69ada commit e674cf0
Show file tree
Hide file tree
Showing 36 changed files with 33,162 additions and 0 deletions.
20 changes: 20 additions & 0 deletions website/.gitignore
@@ -0,0 +1,20 @@
# Dependencies
/node_modules

# Production
/build

# Generated files
.docusaurus
.cache-loader

# Misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
3 changes: 3 additions & 0 deletions website/babel.config.js
@@ -0,0 +1,3 @@
module.exports = {
presets: [require.resolve('@docusaurus/core/lib/babel/preset')],
};
11 changes: 11 additions & 0 deletions website/blog/2019-05-28-hola.md
@@ -0,0 +1,11 @@
---
slug: hola
title: Hola
author: Gao Wei
author_title: Docusaurus Core Team
author_url: https://github.com/wgao19
author_image_url: https://avatars1.githubusercontent.com/u/2055384?v=4
tags: [hola, docusaurus]
---

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque elementum dignissim ultricies. Fusce rhoncus ipsum tempor eros aliquam consequat. Lorem ipsum dolor sit amet
17 changes: 17 additions & 0 deletions website/blog/2019-05-29-hello-world.md
@@ -0,0 +1,17 @@
---
slug: hello-world
title: Hello
author: Endilie Yacop Sucipto
author_title: Maintainer of Docusaurus
author_url: https://github.com/endiliey
author_image_url: https://avatars1.githubusercontent.com/u/17883920?s=460&v=4
tags: [hello, docusaurus]
---

Welcome to this blog. This blog is created with [**Docusaurus 2 alpha**](https://v2.docusaurus.io/).

<!--truncate-->

This is a test post.

A whole bunch of other information.
13 changes: 13 additions & 0 deletions website/blog/2019-05-30-welcome.md
@@ -0,0 +1,13 @@
---
slug: welcome
title: Welcome
author: Yangshun Tay
author_title: Front End Engineer @ Facebook
author_url: https://github.com/yangshun
author_image_url: https://avatars0.githubusercontent.com/u/1315101?s=400&v=4
tags: [facebook, hello, docusaurus]
---

Blog features are powered by the blog plugin. Simply add files to the `blog` directory. It supports tags as well!

Delete the whole directory if you don't want the blog features. As simple as that!
5 changes: 5 additions & 0 deletions website/docs/fp/currying.md
@@ -0,0 +1,5 @@
---
id: currying
title: 'Currying'
sidebar_label: 'Currying (WIP)'
---
10 changes: 10 additions & 0 deletions website/docs/fp/fp.md
@@ -0,0 +1,10 @@
---
id: fp
title: 'Functional Programming (FP)'
sidebar_label: 'Functional Programming (WIP)'
slug: ./
---

## Recursion

## Currying
5 changes: 5 additions & 0 deletions website/docs/fp/recursion.md
@@ -0,0 +1,5 @@
---
id: recursion
title: 'Recursion'
sidebar_label: 'Recursion (WIP)'
---
30 changes: 30 additions & 0 deletions website/docs/functions/array-funs.md
@@ -0,0 +1,30 @@
---
id: array-funs
title: ArrayFuns
sidebar_label: ArrayFuns
---

## `isEmpty`
```java
import j8plus.ArrayFuns;

final String[] emptyArray = {};
final String[] nonEmptyArray = { "abc", "def" };

ArrayFuns.isEmpty(emptyArray); // true
ArrayFuns.isEmpty(nonEmptyArray); // false

```

## `isNotEmpty`
```java
import j8plus.ArrayFuns;

final String[] nonEmptyArray = { "abc", "def" };
final String[] emptyArray = {};

ArrayFuns.isNotEmpty(nonEmptyArray); // true
ArrayFuns.isNotEmpty(emptyArray); // false

```

246 changes: 246 additions & 0 deletions website/docs/functions/funs.md
@@ -0,0 +1,246 @@
---
id: funs
title: Funs (Functions)
sidebar_label: Funs (Functions)
---

**Have lots of fun with Funs!**

Funs class contains methods that can be used with other functions or Stream API (or other Monads).

To use it, just do static import of `j8plus.Funs`.

```java
import static j8plus.Funs.*;
```


## `isNull()`
`isNull()` method returns a `Predicate` which checks if the given parameter is `null`.

```java
System.out.println(isNull().test(null)); // true
System.out.println(isNull().test(1)); // false
System.out.println(isNull().test("abc")); // false
```

* It can be useful when using Stream.

```java
final List<String> listOfNull =
Arrays.asList("a", "b", null, "c", null, null, "d")
.stream()
.filter(isNull())
.collect(toList());

System.out.println("null found: " + listOfNull.size());
```

* Result:

```
null found:3
```

## `isNotNull()`
`isNotNull()` method returns a `Predicate` which checks if the given parameter is not `null`.

```java
System.out.println(isNotNull().test(null)); // false
System.out.println(isNotNull().test(1)); // true
System.out.println(isNotNull().test("abc")); // true
```

* It can be useful when using Stream.

```java
final List<String> listOfNotNullString =
Arrays.asList("a", "b", null, "c", null, null, "d")
.stream()
.filter(isNotNull())
.collect(toList());

System.out.println("list of not null String values: " + listOfNotNullString);
```

* Result:

```
[a,b,c,d]
```

## `reversed()`
`reversed()` returns a `Comparator` which imposes the reversed order of the given Comparator.

### Examples

* With this simple JavaBean

```java
public static class Product {
private Long id;
private String name;
private BigDecimal price;

// getters and setters

public Product price(final BigDecimal price) {
setPrice(price);
return this;
}

// remainder omitted...
}
```

```java
final List<Integer> numbers = Arrays.asList(4, 2, 5, 3, 1);

final Comparator<Integer> intCmp = Integer::compareTo;
final Comparator<Integer> reversedIntCmp = reversed(intCmp);

final List<Integer> numbersInAsc =
numbers.stream()
.sorted(intCmp)
.collect(toList());
System.out.println("Number in ascending order: " + numbersInAsc);
// Number in ascending order: [1, 2, 3, 4, 5]

final List<Integer> numbersInDsc =
numbers.stream()
.sorted(reversedIntCmp)
.collect(toList());
System.out.println("Number in descending order: " + numbersInDsc);
// Number in descending order: [5, 4, 3, 2, 1]
```

```java
final List<Product> products =
Arrays.asList(product(1L, "A", new BigDecimal("30.00")),
product(2L, "B", new BigDecimal("12.50")),
product(3L, "C", new BigDecimal("5.45")));
final List<Product> productsSortedByPriceInAsc =
products
.stream()
.sorted(comparing(Product::getPrice))
.collect(toList());
System.out.println(
"Products sorted by price in ascending order: \n" + productsSortedByPriceInAsc
);
// Products sorted by price in ascending order:
// [
// Product{id=3, name='C', price=5.45},
// Product{id=2, name='B', price=12.50},
// Product{id=1, name='A', price=30.00}
// ]

final List<Product> productsSortedByPriceInDsc =
products
.stream()
.sorted(reversed(comparing(Product::getPrice)))
.collect(toList());
System.out.println(
"Products sorted by price in descending order: \n" + productsSortedByPriceInDsc
);
// Products sorted by price in descending order:
// [
// Product{id=1, name='A', price=30.00},
// Product{id=2, name='B', price=12.50},
// Product{id=3, name='C', price=5.45}
// ]
```

* `.sorted(BigDecimal::compareTo.reversed())` // This results in compile-time error but, the following one doesn't.

```java
final List<BigDecimal> bigDecimalsInDsc =
Arrays.asList(new BigDecimal("3"), new BigDecimal("1"), new BigDecimal("2"))
.stream()
.sorted(reversed(BigDecimal::compareTo))
.collect(toList());
System.out.println("bigDecimalsInDsc: " + bigDecimalsInDsc);
// bigDecimalsInDsc: [3, 2, 1]
```

## `toStringOf`
* `toStringOf` returns a `Function` which returns String. `toStringOf` takes a `Function` as a parameter then combines that with `String::valueOf`. So it will eventually work like

```java
// Parameter function: f
String.valueOf(f.apply(x))
```

* Exmaples

```java
System.out.println(
products.stream()
.map(Product::getPrice)
.collect(joining(", ")) // compile-time error because BigDecimal is not String.
);
```

```java
System.out.println(
products.stream()
.map(toStringOf(Product::getPrice))
.collect(joining(", "))
);
// 30.00, 12.50, 5.45
```

## `satisfying`

`satisfying` takes `BiPredicate<O, T>` and an additional parameter of type `T` then returns a `Predicate<O>`. This is meant to be used with method references to simplifying filtering. It would be much clear with some examples.

* Examples

Where there is a list of String and you want to filter in all String values start with a certain word. If you do it like this using lambda expressions.

```java
System.out.println(
Arrays.asList("Hello world", "Hello Kevin", "Hi world", "Hey", "Hello")
.stream()
.filter(s -> s.startsWith("Hello"))
.collect(toList())
);
// [Hello world, Hello Kevin, Hello]
```
Because `startWith()` method takes a parameter, you can't use method reference.
```java
.filter(String::startsWith("Hello")) // You cannot pass parameters to a method reference.
```

However, if you use `satisfying()` method, you can. Just like this.
```java
System.out.println(
Arrays.asList("Hello world", "Hello Kevin", "Hi world", "Hey", "Hello")
.stream()
.filter(satisfying(String::startsWith, "Hello"))
.collect(toList())
);
// [Hello world, Hello Kevin, Hello]
```

## `applying`

```java
final List<Product> products = Arrays.asList(
product(1L, "A", $("30.00")),
product(2L, "B", $("12.50")),
product(3L, "C", $("5.45"))
);

final BigDecimal specialPrice = new BigDecimal("10.00");
System.out.println(
products
.stream()
.map(applying(Product::price, specialPrice))
.collect(toList())
);
// [
// Product{id=1, name='A', price=10.00},
// Product{id=2, name='B', price=10.00},
// Product{id=3, name='C', price=10.00}
// ]
```

0 comments on commit e674cf0

Please sign in to comment.