gum
vs. Preludezx
vs. Preludecurl
vs. Prelude- Python vs. Prelude
- NumPy vs. Prelude
- Pandas vs. Prelude
- JavaScript vs. Prelude
Note gum is a tool for glamorous shell scripts
gum
gum input
Prelude
input '>'
gum
gum input --password
Prelude
Note Default label is
input:
input '>' -Secret
gum
gum choose (1..10)
Prelude
1..10 | menu
gum
gum choose (1..10) --limit 10
Prelude
1..10 | menu -MultiSelect
gum
gum file .
Prelude
menu -FolderContent
Note
zx
code goes in files with the.mjs
extension.Prelude
code goes in files with the.ps1
extension.
zx
Note
zx
makes the chalk package available
#!/usr/bin/env zx
console.log(chalk.blue('Hello world!'))
Prelude
#Requires -Modules Prelude
'Hello World' | Write-Color -Blue
zx
Note
zx
makes the fs-extra package available
#!/usr/bin/env zx
let {version} = await fs.readJson('./package.json')
Prelude
#Requires -Modules Prelude
$Version = Get-Content .\package.json | ConvertFrom-Json | prop version
Note Prelude works on Windows and Linux
curl
curl --header "Content-Type: application/json" \
--request POST \
--data '{"answer": 42}' \
https://example.com
Prelude
@{ answer = 42 } | basicauth 'https://example.com' -Post -Header @{ 'Content-Type' = 'application/json' }
curl
curl -H "Authorization: OAuth ACCESS_TOKEN" https://example.com
Prelude
basicauth 'https://example.com' -Token 'ACCESS_TOKEN'
data.csv
foo,bar
blue,small
red,medium
green,large
Python
import csv
with open('data.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
for row in csv_reader:
print(row[0])
Prelude
Get-Content 'data.csv' | ConvertFrom-Csv | ForEach-Object { $_.foo }
data.json
{
"PowerLevel": 9001
}
Python
import json
with open('data.json', 'r') as myfile:
data = myfile.read()
obj = json.loads(data)
print(obj["PowerLevel"])
Prelude
Import-Module 'Prelude'
Get-Content 'data.json' | ConvertFrom-Json | prop 'PowerLevel'
NumPy
import numpy as np
ndArray = np.arange(9).reshape(3, 3)
a = np.map(ndArray)
Prelude
Import-Module 'Prelude'
$A = 1..9 | matrix 3,3
NumPy
import numpy as np
x = np.array([[1, 2], [3, 4]])
y = np.array([[10, 20], [30, 40]])
product = np.dot(x, y)
Prelude
Import-Module 'Prelude'
$X = 1, 2, 3, 4 | matrix
$Y = 10, 20, 30, 40 | matrix
$Product = $X * $Y
NumPy
import numpy as np
x = np.array([[4, 8], [7, 9]])
determinant = np.linalg.det(x)
inverse = np.linalg.inv(x)
Prelude
Import-Module 'Prelude'
$X = 4, 8, 7, 9 | matrix
$Determinant = $X.Det()
$Inverse = $X.Inverse()
Simple linear regression (with SciKit-Learn)
NumPy and SciKit-Learn
import numpy as np
from sklearn.linear_model import LinearRegression
x = np.array([5, 15, 25, 35, 45, 55]).reshape((-1, 1))
y = np.array([5, 20, 14, 32, 22, 38])
model = LinearRegression().fit(x, y)
print('Intercept:', model.intercept_)
print('Slope:', model.coef_)
Prelude
Import-Module 'Prelude'
$X0 = matrix -Unit 6,1
$X1 = 5, 15, 25, 35, 45, 55 | matrix 6,1
$X = $X0.Augment($X1)
$Y = 5, 20, 14, 32, 22, 38 | matrix 6,1
$B = ($X.Transpose() * $X).Inverse() * ($X.Transpose() * $Y)
"Intercept: $($B[0].Real)" | Write-Color -Green
"Slope: $($B[1].Real)" | Write-Color -Green
data.csv
foo,bar
blue,small
red,medium
green,large
Pandas
import pandas as pd
data = pd.read_csv("data.csv")
col_map = {
"foo": "color",
"bar": "size"
}
data = data.rename(columns=col_map)
Prelude
Import-Module 'Prelude'
$Data = Get-Content 'data.csv' | ConvertFrom-Csv
$Lookup = @{
color = 'foo'
size = 'bar'
}
$Data = $Data | transform $Lookup
JavaScript
const result = Math.max(...(['always', 'look', 'on', 'the', 'bright', 'side', 'of', 'life'].map(el => el.length)));
console.log(result);
Prelude
Import-Module 'Prelude'
'always', 'look', 'on', 'the', 'bright', 'side', 'of', 'life' | prop Length | max
JavaScript
const partition = (arr, predicate) => arr.reduce((acc, i) => (acc[predicate(i) ? 0 : 1].push(i), acc), [[], []]);
const isEven = n => n % 2 === 0
const result = partition([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], isEven);
console.log(result);
Prelude
Import-Module 'Prelude'
$IsEven = { Param($X) $X % 2 -eq 0 }
1..10 | partition $IsEven
JavaScript
const chunk = (arr, size) => arr.reduce((acc, e, i) => (i % size ? acc[acc.length - 1].push(e) : acc.push([e]), acc), []);
const result = chunk([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 3);
console.log(result);
Prelude
Import-Module 'Prelude'
1..10 | chunk -s 3
JavaScript
const zip = (...arr) => Array.from({length: Math.max(...arr.map(a => a.length))}, (_, i) => arr.map(a => a[i]));
const result = zip(['a', 'b', 'c', 'd', 'e'], [1, 2, 3, 4, 5]);
console.log(result);
Prelude
Import-Module 'Prelude'
@('a', 'b', 'c', 'd', 'e'), (1..5) | zip
JavaScript
const unzip = arr => arr.reduce((acc, c) => (c.forEach((v, i) => acc[i].push(v)), acc), Array.from({length: Math.max(...arr.map(a => a.length))}, (_) => []));
const result = unzip([['a', 1], ['b', 2], ['c', 3], ['d', 4], ['e', 5]]);
console.log(result);
Prelude
Import-Module 'Prelude'
@('a', 1), @('b', 2), @('c', 3), @('d', 4), @('e', 5) | unzip