Skip to content

Latest commit

 

History

History
430 lines (362 loc) · 9.59 KB

Compare.md

File metadata and controls

430 lines (362 loc) · 9.59 KB

Comparison Examples

gum vs. Prelude

Note gum is a tool for glamorous shell scripts

Prompt for user input

gum

gum input

Prelude

input '>'

Prompt for sensitive input

gum

gum input --password

Prelude

Note Default label is input:

input '>' -Secret

Choose an option from a list of choices

gum

gum choose (1..10)

Prelude

1..10 | menu

Choose multiple options from a list of choices

gum

gum choose (1..10) --limit 10

Prelude

1..10 | menu -MultiSelect

Prompt the use to select a file/folder from the file tree

gum

gum file .

Prelude

menu -FolderContent

zx vs. Prelude

Note zx code goes in files with the .mjs extension. Prelude code goes in files with the .ps1 extension.

Print colored text to the terminal

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

Get version from package.json file

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

curl vs. Prelude

Note Prelude works on Windows and Linux

POST JSON data with cURL

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' }

Set authorization header for request

curl

curl -H "Authorization: OAuth ACCESS_TOKEN" https://example.com

Prelude

basicauth 'https://example.com' -Token 'ACCESS_TOKEN'

Python vs. Prelude

Read a CSV file

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 }

Read a JSON file

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 vs. Prelude

Create an matrix from a range of numbers

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

Calculate dot product of two matrices

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

Calculate matrix determinant or inverse

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

Pandas vs. Prelude

Rename column names

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 vs. Prelude

Find the length of the longest word in a list

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

Partition an array

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

Chunk an array into smaller arrays

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

Zip two arrays into a single array

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

Unzip an array into two arrays

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