1
Fork 0
mirror of https://github.com/RGBCube/nu_scripts synced 2025-08-01 06:37:46 +00:00

Merge pull request #101 from skelly37/main

Initial PR for mathematical functions and constants
This commit is contained in:
JT 2021-10-26 20:46:30 +13:00 committed by GitHub
commit c9ccc80ab6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 82 additions and 0 deletions

16
cool_oneliners/dict.nu Normal file
View file

@ -0,0 +1,16 @@
#As the name suggests, the function returns you the definition and example of the sought English word
#(or breaks if it can't find the word because the API uses different columns for successful and invalid searches)
#usage: dict word
#usage: dict "word with space"
def dict [word: string] {
let link = (build-string 'https://api.dictionaryapi.dev/api/v2/entries/en/' ($word|str find-replace ' ' '%20'))
let output = (fetch $link |
rename word)
if $output.word == "No Definitions Found" {echo $output.word} {echo $output |
get meanings.definitions |
select definition example
}
}

View file

@ -0,0 +1,29 @@
#As in the file name, the script is designed to conveniently handle any archive format
#usage: extract archive_name
def extract [name:string] {
let exten = [ [ex com];
['.tar.bz2' 'tar xjf']
['.tar.gz' 'tar xzf']
['.bz2' 'bunzip2']
['.rar' 'unrar x']
['.tbz2' 'tar xjf']
['.tgz' 'tar xzf']
['.zip' 'unzip']
['.7z' '/usr/bin/7z x']
['.deb' 'ar x']
['.tar.xz' 'tar xvf']
['.tar.zst' 'tar xvf']
['.tar' 'tar xvf']
['.gz' 'gunzip']
['.Z' 'uncompress']
]
let command = ($exten|where $name =~ $it.ex|first)
if ($command|empty?) {
echo 'Error! Unsupported file extension'
} {
nu -c (build-string $command.com ' ' $name)
}
}

37
maths/math_functions.nu Normal file
View file

@ -0,0 +1,37 @@
#!/usr/bin/nu
# made by @skelly37 for everyone :)
#
# (sqrt skipped because of math module from nushell)
#
# BE CAREFUL WITH ROOTS MADE ON BIG NUMBERS (10 digits and more) — the result is rounded so you may get wrong result.
# just modify the round -p or calculate it manually
# cube root
def croot [num: number] {
$num ** ( 1 / 3 ) | math round -p 10
}
# root with custom denominator, e.g. 2 ** 1/4
def root [ denominator: number, num: number ] {
$num ** ( 1 / $denominator ) | math round -p 10
}
# totally custom root, e.g. 2 ** 3/8
def aroot [ scaler: number, denominator: number, num: number ] {
$num ** ($scaler / $denominator) | math round -p 10
}
#discriminant of a quadratic equation
def delta [ a: number, b: number, c: number ] {
( $b ** 2 ) - ( 4 * $a * $c)
}
#factorial of a given number, works fine only for n<21 (integer range)
def fact [num:number] {
if $num >= 0 { if $num < 2 {$num} {seq 2 $num | math product} } { echo 'Error: can only calculate non-negative integers'}
}
#TODO:
# -sin, cos, tan table for 0-90 degrees
# -radians to degrees and reversed
# -sin, cos, tan, ctg, sec, cosec functions