From 8b04417a4d4642107b29f39e4ea778d1303f6bcd Mon Sep 17 00:00:00 2001 From: Kamil Date: Thu, 24 Mar 2022 19:49:01 +0100 Subject: [PATCH] Ported my scripts to 0.60 (#189) * dict.nu ready for 0.60 * wolfram didn't require any changes * extractor works flawlessy on 0.60 * maths ported to 0.60 --- api_wrappers/wolframalpha.nu | 19 +++++++++++++++ cool-oneliners/dict.nu | 9 ++++--- data_extraction/ultimate_extractor.nu | 27 +++++++++++++++++++++ maths/math_functions.nu | 35 +++++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 api_wrappers/wolframalpha.nu create mode 100644 data_extraction/ultimate_extractor.nu create mode 100644 maths/math_functions.nu diff --git a/api_wrappers/wolframalpha.nu b/api_wrappers/wolframalpha.nu new file mode 100644 index 0000000..5383c7e --- /dev/null +++ b/api_wrappers/wolframalpha.nu @@ -0,0 +1,19 @@ +#Fetch simple anwser from WolframAlpha API +def wolfram [...query #Your query +] { + let appID = #YOUR APP_ID + let query_string = ($query | str collect " ") + let result = (fetch ("https://api.wolframalpha.com/v1/result?" + ([[appid i]; [$appID $query_string]] | to url))) + $result + "" +} + +#Fetch image with full anwser from WolframAlpha API +def wolframimg [...query #Your query +] { + let appID = #YOUR APP_ID + let query_string = ($query | str collect " ") + let filename = ($query_string + ".png") + let link = ("https://api.wolframalpha.com/v1/simple?" + ([[appid i]; [$appID $query_string]] | to url) + "&background=F5F5F5&fontsize=20") + fetch $link | save $filename + echo ("Query result saved in file: " + $filename) +} diff --git a/cool-oneliners/dict.nu b/cool-oneliners/dict.nu index 5f5aa30..d892b7d 100644 --- a/cool-oneliners/dict.nu +++ b/cool-oneliners/dict.nu @@ -7,8 +7,11 @@ def dict [...word #word(s) to query the dictionary API but they have to make sen rename word) let w = ($output.word | first) - if $w == "No Definitions Found" {echo $output.word} else {echo $output | - get meanings.definitions | - select definition example + if $w == "No Definitions Found" { + echo $output.word + } else { + echo $output.meanings.definitions | + flatten | flatten | + select definition example } } diff --git a/data_extraction/ultimate_extractor.nu b/data_extraction/ultimate_extractor.nu new file mode 100644 index 0000000..e5ee722 --- /dev/null +++ b/data_extraction/ultimate_extractor.nu @@ -0,0 +1,27 @@ +#Function to extract archives with different extensions +def extract [name:string #name of the archive to extract +] { + 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' + } else { + nu -c (build-string $command.com ' ' $name) + } +} diff --git a/maths/math_functions.nu b/maths/math_functions.nu new file mode 100644 index 0000000..2479e27 --- /dev/null +++ b/maths/math_functions.nu @@ -0,0 +1,35 @@ +#Root with a custom denominator +def root [ denominator, num ] { + $num ** ( 1 / $denominator ) | math round -p 10 +} + +#Cube root +def croot [num] { + $num ** ( 1 / 3 ) | math round -p 10 +} + +#Root with a custom scaler and denominator +def aroot [ scaler, denominator, num] { + $num ** ($scaler / $denominator) | math round -p 10 +} + +#calculate delta of the quadratic function +def delta [ a,#x^2 factor +b, #x factor +c #the rest +] { + ( $b ** 2 ) - ( 4 * $a * $c) +} + +#Factorial of the given number +def fact [num: int] { + if $num >= 0 { + if $num < 2 { + $num + } else { + seq 2 $num | math product + } + } else { + echo 'Error: can only calculate non-negative integers' + } +}