This commit is contained in:
Rico Sta. Cruz 2016-06-03 07:01:18 +08:00
parent 6882272547
commit 4861e0ff2e
No known key found for this signature in database
GPG Key ID: CAAD38AE2962619A
4 changed files with 158 additions and 77 deletions

View File

@ -45,8 +45,8 @@ re = reduceReducers(
re(10, { number: 2 }) //=> 14 re(10, { number: 2 }) //=> 14
``` ```
### [redux-batched-updates](https://www.npmjs.com/package/redux-batched-updates) ### [redux-logger](https://github.com/evgenyrodionov/redux-logger)
Batch React updates that occur as a result of Redux dispatches. Uses `react/addons` (v0.12+). Debounces dispatch() calls. Logs actions to your console.
Async Async
----- -----

172
elixir.md
View File

@ -56,72 +56,12 @@ left <> right # concat string/binary
left =~ right # regexp left =~ right # regexp
``` ```
### Numbers
```
abs(n)
rem(a, b) # remainder (modulo)
div(a, b) # integer division
round(n)
```
### Functions
```
apply(fn, args)
apply(module, fn, args)
```
### Inspecting ### Inspecting
```elixir ```elixir
inspect(arg, opts \\ []) inspect(arg, opts \\ [])
``` ```
### Tuples
```elixir
elem(tuple, 1) # like tuple[1]
put_elem(tuple, index, value)
tuple_size(tuple)
```
## Maps
```elixir
import Map
map = %{a: "Apple"} # atom keys (:a)
map = %{"a" => "Apple"} # string keys ("a")
map = %{map | a: "Atis"} # key must exist
map[:a]
map.a # same
```
### Updating
```elixir
put(map, :b, "Banana")
merge(map, %{b: "Banana"})
update(map, :a, &(&1 + 1))
update(map, :a, fun a -> a + 1 end)
{old, new} = get_and_update(map, :a, &(&1 || "default"))
# Deep functions (_in)
put_in(map, [:b, :c], "Banana")
put_in(map[:b][:c], "Banana") # via macros
get_and_update_in(users, ["john", :age], &{&1, &1 + 1})
```
### Reading
```
Map.keys(map)
```
## String ## String
```elixir ```elixir
@ -135,6 +75,15 @@ str |> capitalize() #=> "Hello"
str |> match(regex) str |> match(regex)
``` ```
## Numbers
```elixir
abs(n)
rem(a, b) # remainder (modulo)
div(a, b) # integer division
round(n)
```
### Float ### Float
```elixir ```elixir
@ -178,33 +127,64 @@ Float.to_string(34.1) #=> "3.4100e+01"
Float.to_string(34.1, [decimals: 2, compact: true]) #=> "34.1" Float.to_string(34.1, [decimals: 2, compact: true]) #=> "34.1"
``` ```
### Map ## Map
```js ```elixir
map = %{name: "hi"} # atom keys (:name)
map = %{"name" => "hi"} # string keys ("name")
```
### Updating
```elixir
import Map import Map
map = %{id: 1, name: "hi"}
delete(map, :name) #=> "hi" map = %{map | name: "yo"} # key must exist
pop(map, :name) #=> %{id: 1}
put(map, :id, 2) #=> %{id: 2, name: "hi"} put(map, :id, 2) #=> %{id: 2, name: "hi"}
put_new(map, :id, 2) # only if `id` doesn't exist put_new(map, :id, 2) # only if `id` doesn't exist (`||=`)
put(map, :b, "Banana")
merge(map, %{b: "Banana"})
update(map, :a, &(&1 + 1))
update(map, :a, fun a -> a + 1 end)
{old, new} = get_and_update(map, :a, &(&1 || "default"))
```
### Deleting
```elixir
delete(map, :name) #=> "hi"
pop(map, :name) #=> %{id: 1}
```
### Reading
```elixir
get(map, :id) #=> 1 get(map, :id) #=> 1
keys(map) #=> [:id, :name] keys(map) #=> [:id, :name]
values(map) #=> [1, "hi"] values(map) #=> [1, "hi"]
to_list(map) #=> [id: 1, name: "hi"] to_list(map) #=> [id: 1, name: "hi"]
#=> [{:id, 1}, {:name, "hi"}] #=> [{:id, 1}, {:name, "hi"}]
```
merge(map, %{name: "hello"}) ### Deep
```elixir
put_in(map, [:b, :c], "Banana")
put_in(map[:b][:c], "Banana") # via macros
get_and_update_in(users, ["john", :age], &{&1, &1 + 1})
```
### Constructing
```elixir
Map.new([{:b, 1}, {:a, 2}]) Map.new([{:b, 1}, {:a, 2}])
Map.new([a: 1, b: 2]) Map.new([a: 1, b: 2])
Map.new([:a, :b], fn x -> {x, x} end) #=> %{a: :a, b: :b} Map.new([:a, :b], fn x -> {x, x} end) #=> %{a: :a, b: :b}
``` ```
### List ## List
Also see [Enum](#enum). Also see [Enum](#enum).
@ -220,7 +200,7 @@ flatten(list)
flatten(list, tail) flatten(list, tail)
``` ```
### Enum ## Enum
```elixir ```elixir
# consider streams instead # consider streams instead
@ -242,7 +222,48 @@ any?(list) #=> true
concat(list, [:d]) #=> [:d] concat(list, [:d]) #=> [:d]
``` ```
There's really way too many things, just see <https://learnxinyminutes.com/docs/elixir/>. ## Tuples
```elixir
tuple = { :a, :b }
elem(tuple, 1) # like tuple[1]
put_elem(tuple, index, value)
tuple_size(tuple)
```
### Keyword lists
```elixir
list = [{ :name, "John" }, { :age, 15 }]
list.name
```
## Functions
### Lambdas
```elixir
square = fn n -> n*n end
square.(20)
```
### & syntax
```elixir
square = &(&1 * &1)
square.(20)
square = &Math.square/1
```
### Running
```elixir
fun.(args)
apply(fun, args)
apply(module, fun, args)
```
## Syntax ## Syntax
@ -264,8 +285,8 @@ end
```elixir ```elixir
def join(a, b \\ nil) def join(a, b \\ nil)
def join(a, b) when is_nil(b) do: a end def join(a, b) when is_nil(b) do: a
def join(a, b) do: a <> b; end def join(a, b) do: a <> b
``` ```
## Protocols ## Protocols
@ -326,7 +347,7 @@ for dir <- dirs,
file <- File.ls!(dir), # nested comprehension file <- File.ls!(dir), # nested comprehension
path = Path.join(dir, file), # invoked path = Path.join(dir, file), # invoked
File.regular?(path) do # condition File.regular?(path) do # condition
IO.puts(file, IO.puts(file)
end end
``` ```
@ -400,3 +421,8 @@ end
``` ```
[Reference](http://elixir-lang.org/docs/stable/elixir/Module.html) [Reference](http://elixir-lang.org/docs/stable/elixir/Module.html)
## References
- [Learn Elixir in Y minutes](https://learnxinyminutes.com/docs/elixir/)

55
js-fetch.md Normal file
View File

@ -0,0 +1,55 @@
---
title: fetch()
category: JavaScript
---
```js
fetch('/data.json')
.then(response => response.json())
.catch(err => ...)
```
### Response
```js
fetch('/data.json')
.then(res => {
res.text() // response body
res.json() // parse via JSON
res.status //=> 200
res.statusText //=> 'OK'
res.redirected //=> false
res.ok //=> true
res.url //=> 'http://site.com/data.json'
res.type //=> 'basic' ('cors' 'default' 'error' 'opaque' 'opaqueredirect')
res.headers.get('Content-Type')
})
```
### Request options
```js
fetch('/data.json', {
method: 'post',
body: new FormData(form), // post body
body: JSON.stringify(...),
headers: {
'Accept': 'application/json'
}
})
```
##
## Using with node.js
```js
var fetch = require('isomorphic-fetch')
```
## References
- <https://fetch.spec.whatwg.org/>
- <https://www.npmjs.com/package/whatwg-fetch>

View File

@ -13,8 +13,8 @@ conn.request_path #=> "/posts/1"
conn.query_string #=> "utm_source=twitter" conn.query_string #=> "utm_source=twitter"
conn.port #=> 80 conn.port #=> 80
conn.scheme #=> :http conn.scheme #=> :http
conn.peer #=> {{127, 0, 0, 1}, 12345} conn.peer #=> { {127, 0, 0, 1}, 12345 }
conn.remote_ip #=> {151, 236, 219, 228} conn.remote_ip #=> { 151, 236, 219, 228 }
conn.req_headers #=> [{"content-type", "text/plain"}] conn.req_headers #=> [{"content-type", "text/plain"}]
``` ```