gumvs. Preludezxvs. Preludecurlvs. Prelude- Python vs. Prelude
- NumPy vs. Prelude
- Pandas vs. Prelude
- JavaScript vs. Prelude
Note gum is a tool for glamorous shell scripts
gum
gum inputPrelude
input '>'gum
gum input --passwordPrelude
Note Default label is
input:
input '>' -Secretgum
gum choose (1..10)Prelude
1..10 | menugum
gum choose (1..10) --limit 10Prelude
1..10 | menu -MultiSelectgum
gum file .Prelude
menu -FolderContentNote
zxcode goes in files with the.mjsextension.Preludecode goes in files with the.ps1extension.
zx
Note
zxmakes the chalk package available
#!/usr/bin/env zx
console.log(chalk.blue('Hello world!'))Prelude
#Requires -Modules Prelude
'Hello World' | Write-Color -Bluezx
Note
zxmakes 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 versionNote Prelude works on Windows and Linux
curl
curl --header "Content-Type: application/json" \
--request POST \
--data '{"answer": 42}' \
https://example.comPrelude
@{ answer = 42 } | basicauth 'https://example.com' -Post -Header @{ 'Content-Type' = 'application/json' }curl
curl -H "Authorization: OAuth ACCESS_TOKEN" https://example.comPrelude
basicauth 'https://example.com' -Token 'ACCESS_TOKEN'data.csv
foo,bar
blue,small
red,medium
green,largePython
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,3NumPy
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 * $YNumPy
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 -Greendata.csv
foo,bar
blue,small
red,medium
green,largePandas
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 $LookupJavaScript
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 | maxJavaScript
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 $IsEvenJavaScript
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 3JavaScript
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) | zipJavaScript
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