Skip to content
Raine Virta edited this page Mar 11, 2017 · 40 revisions

Feel free to contribute to this document with ramda-cli snippets that have helped you.

Recipes

Create a markdown formatted table of Ramda's functions

Output from ramda-cli can be piped to markdown-cli-table to create a Markdown formatted table.

npm install -g markdown-table-cli
curl -s https://raine.github.io/ramda-json-docs/latest.json | \
  R 'project <[ name sig category ]>' \
    'map evolve sig: (-> it and "`#it`"), name: -> "[`#it`](http://ramdajs.com/docs/##it)"' \
  | md-table
name sig category
add Number -> Number -> Number Math
adjust (a -> a) -> Number -> [a] -> [a] List
always a -> (* -> a) Function
aperture Number -> [a] -> [[a]] List

Merge JSON files into a single object

cat *.json | R --slurp merge-all

Unwrap a list of objects into Line Delimited JSON

cat data.json | R --unslurp --compact identity

Read filenames from stdin as an object of {filename: body}

find . -name '*.txt' | R -r --slurp 'map -> (it): read-file it' merge-all

Inspect MongoDB collection as a table

mongoexport -d test -c zips --jsonArray -q '{state: "NY"}' --sort '{pop: -1}' |\
  R identity -o table | less -F

Data: http://media.mongodb.org/zips.json

The result looks something like this:

Create an <img> tag based on identify output

$ identify test.png
test.png PNG 594x472 594x472+0+0 8-bit sRGB 187KB 0.000u 0:00.009

$ img-tag test.png
<img src="test.png" width="594" height="472">
#!/usr/bin/env bash

identify "$1" | R -Rr \
  'match /(^.+?) .+\b(\d+)x(\d+)\b/' \
  'tail' \
  'zip-obj [\src, \width, \height]' \
  'h \img, _' \
  '.outer-HTML' \
  'replace "<\/img>", ""'

I use this to create img tags for retina images with dimensions of half the size (for GitHub READMEs). For that you have to add this after the zip-obj line.

'f=parse-int >> (/ 2); evolve width: f, height: f' \

Get a table of things occurring per minute

By using countBy, we get counts for elements of a list according to how many match the supplied function.

In this case, we pass a function that parses a time field and formats it to %H %M with strftime module.

query-es 'url:catpics' | R --import strftime -s -o table -c \
  'count-by -> strftime("%R", new Date it.time)'
┌───────┬────┐
│ 11:58 │ 10 │
│ 11:59 │ 26 │
│ 12:00 │ 32 │
│ 12:01 │ 96 │
│ 12:02 │ 80 │
│ 12:03 │ 44 │
│ 12:04 │ 46 │
│ 12:05 │ 66 │
└───────┴────┘

Playing around with country-data

Repository: https://github.com/samayo/country-data

git clone git@github.com:samayo/country-data.git
Merge data from multiple json files
cat src/country-{population,continent,domain-tld}.json | R --slurp -o table -c \
  'unnest' \
  'group-by (.country)' \
  'map-obj merge-all' \
  'values'
┌──────────────────────┬────────────┬───────────────┬─────┐
│ country              │ population │ continent     │ tld │
├──────────────────────┼────────────┼───────────────┼─────┤
│ Aruba                │ 103000     │ North America │ .aw │
│ Afghanistan          │ 22720000   │ Asia          │ .af │
│ Angola               │ 12878000   │ Africa        │ .ao │
│ Anguilla             │ 8000       │ North America │ .ai │
│ Albania              │ 3401200    │ Europe        │ .al │
│ Andorra              │ 78000      │ Europe        │ .ad │
│ Netherlands Antilles │ 217000     │ North America │ .an │
│ United Arab Emirates │ 2441000    │ Asia          │ .ae │
│ Argentina            │ 37032000   │ South America │ .ar │
│ Armenia              │ 3520000    │ Asia          │ .am │
└──────────────────────┴────────────┴───────────────┴─────┘
Sort by population and format the number with approximate-number
cat src/country-population.json | R --import approximate-number -o table -c \
  'sort-by prop \population'
  'map evolve population: approximate-number'
  'reverse'
┌────────────────────┬────────────┐
│ country            │ population │
├────────────────────┼────────────┤
│ China              │ 1.3b       │
│ India              │ 1b         │
│ United States      │ 278m       │
│ Indonesia          │ 212m       │
│ Brazil             │ 170m       │
│ Pakistan           │ 156m       │
│ Russian Federation │ 147m       │
│ Bangladesh         │ 129m       │
│ Japan              │ 127m       │
│ Nigeria            │ 112m       │
└────────────────────┴────────────┘

View a npm module on GitHub

https://twitter.com/rane/status/647391424930050048/

Query S3 API and push total object count and size to statsd

#!/usr/bin/env bash

# First aws command gets the immediate prefixes under photo/, second the sum
# of objects' sizes and count for each prefix.
aws s3api list-objects --bucket my-bucket --prefix photo/ --delimiter '/' |\
  R --raw-output '.CommonPrefixes' 'pluck \Prefix' 'map split "/"' 'map nth 1' |\
  xargs -P4 -I % aws s3api list-objects --bucket my-bucket --prefix photo/% \
    --query '{%: [sum(Contents[].Size), length(Contents[])]}' |\
  R --raw-output --slurp \
    merge-all to-pairs \
    'map -> zip-with ((k,v) -> "#k.#{it.0}:#v|g"), <[ s3_total_size s3_count ]>, it.1' \
    flatten \
    "map concat '$statsd__prefix.'" |\
  xargs -t -n1 ./statsd-client
./statsd-client photos.s3_total_size.BR:125124255|g
./statsd-client photos.s3_count.BR:5124|g
./statsd-client photos.s3_total_size.FI:3017284917|g
./statsd-client photos.s3_count.FI:2451|g
./statsd-client photos.s3_total_size.DE:91873259|g