June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -1,14 +1,13 @@
use framework "Foundation" -- Yosemite onwards, for the toLowerCase() function
-- Kelvin to other scale
-- KELVIN TO OTHER SCALE -----------------------------------------------------
-- kelvinAs :: ScaleName -> Num -> Num
on kelvinAs(strOtherScale, n)
heatBabel(n, "Kelvin", strOtherScale)
end kelvinAs
-- More general conversion
-- MORE GENERAL CONVERSION ---------------------------------------------------
-- heatBabel :: n -> ScaleName -> ScaleName -> Num
on heatBabel(n, strFromScale, strToScale)
@ -17,7 +16,7 @@ on heatBabel(n, strFromScale, strToScale)
set fahr to 459.67
script reading
on lambda(x, strFrom)
on |λ|(x, strFrom)
if strFrom = "k" then
x as real
else if strFrom = "c" then
@ -27,11 +26,11 @@ on heatBabel(n, strFromScale, strToScale)
else
x / ratio
end if
end lambda
end |λ|
end script
script writing
on lambda(x, strTo)
on |λ|(x, strTo)
if strTo = "k" then
x
else if strTo = "c" then
@ -41,22 +40,21 @@ on heatBabel(n, strFromScale, strToScale)
else
x * ratio
end if
end lambda
end |λ|
end script
writing's lambda(reading's lambda(n, ¬
toLowerCase(text 1 of strFromScale)), ¬
toLowerCase(text 1 of strToScale))
writing's |λ|(reading's |λ|(n, ¬
toLower(text 1 of strFromScale)), ¬
toLower(text 1 of strToScale))
end heatBabel
-- TEST
-- TEST ----------------------------------------------------------------------
on kelvinTranslations(n)
script translations
on lambda(x)
on |λ|(x)
{x, kelvinAs(x, n)}
end lambda
end |λ|
end script
map(translations, {"K", "C", "F", "R"})
@ -64,24 +62,16 @@ end kelvinTranslations
on run
script tabbed
on lambda(x)
on |λ|(x)
intercalate(tab, x)
end lambda
end |λ|
end script
intercalate(linefeed, map(tabbed, kelvinTranslations(21)))
end run
-- GENERIC LIBRARY FUNCTIONS
-- toLowerCase :: String -> String
on toLowerCase(str)
set ca to current application
((ca's NSString's stringWithString:(str))'s ¬
lowercaseStringWithLocale:(ca's NSLocale's currentLocale())) as text
end toLowerCase
-- GENERIC FUNCTIONS ---------------------------------------------------------
-- intercalate :: Text -> [Text] -> Text
on intercalate(strText, lstText)
@ -97,7 +87,7 @@ on map(f, xs)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to lambda(item i of xs, i, xs)
set end of lst to |λ|(item i of xs, i, xs)
end repeat
return lst
end tell
@ -110,7 +100,14 @@ on mReturn(f)
f
else
script
property lambda : f
property |λ| : f
end script
end if
end mReturn
-- toLower :: String -> String
on toLower(str)
set ca to current application
((ca's NSString's stringWithString:(str))'s ¬
lowercaseStringWithLocale:(ca's NSLocale's currentLocale())) as text
end toLower

View file

@ -0,0 +1,20 @@
; ### USAGE - TESTING PURPOSES ONLY
Local Const $_KELVIN = 21
ConsoleWrite("Kelvin: " & $_KELVIN & @CRLF)
ConsoleWrite("Kelvin: " & Kelvin(21, "C") & @CRLF)
ConsoleWrite("Kelvin: " & Kelvin(21, "F") & @CRLF)
ConsoleWrite("Kelvin: " & Kelvin(21, "R") & @CRLF)
; ### KELVIN TEMPERATURE CONVERSIONS
Func Kelvin($degrees, $conversion)
Select
Case $conversion = "C"
Return Round($degrees - 273.15, 2)
Case $conversion = "F"
Return Round(($degrees * 1.8) - 459.67, 2)
Case $conversion = "R"
Return Round($degrees * 1.8, 2)
EndSelect
EndFunc ;==> Kelvin

View file

@ -0,0 +1,27 @@
import extensions.
convertKelvinToFahrenheit = (:x)(x * 1.8r - 459.6r).
convertKelvinToRankine = (:x)(x * 1.8r).
convertKelvinToCelsius = (:x)(x - 273.15r).
program =
[
console print("Enter a Kelvin Temperature: ").
var inputVal := console readLine.
real kelvinTemp := 0.0r.
try(realConvertor convert literal:inputVal vreal:kelvinTemp)
{
on(Exception e)
[
console printLine("Invalid input value: ", inputVal).
AbortException new; raise
]
}.
console printLine("Kelvin: ", kelvinTemp).
console printLine("Fahrenheit: ", convertKelvinToFahrenheit(kelvinTemp)).
console printLine("Rankine: ", convertKelvinToRankine(kelvinTemp)).
console printLine("Celsius: ", convertKelvinToCelsius(kelvinTemp)).
console readKey
].

View file

@ -0,0 +1,12 @@
USING: combinators formatting kernel math ;
IN: rosetta-code.temperature
: k>c ( kelvin -- celsius ) 273.15 - ;
: k>r ( kelvin -- rankine ) 9/5 * ;
: k>f ( kelvin -- fahrenheit ) k>r 459.67 - ;
: convert ( kelvin -- )
{ [ ] [ k>c ] [ k>f ] [ k>r ] } cleave
"K %.2f\nC %.2f\nF %.2f\nR %.2f\n" printf ;
21 convert

View file

@ -0,0 +1,12 @@
#!/bin/ksh
# Temperature conversion
typeset tt[1]=0.00 tt[2]=273.15 tt[3]=373.15
for i in {1..3}
do
((t=tt[i]))
echo $i
echo "Kelvin: $t K"
echo "Celsius: $((t-273.15)) C"
echo "Fahrenheit: $((t*18/10-459.67)) F"
echo "Rankine: $((t*18/10)) R"
done

View file

@ -0,0 +1,12 @@
#!/bin/bash
# Temperature conversion
tt[1]=0.00; tt[2]=273.15; tt[3]=373.15
for i in {1..3}
do
t=${tt[$i]}
echo $i
echo "Kelvin: $t K"
echo "Celsius: $(bc<<<"scale=2;$t-273.15") C"
echo "Fahrenheit: $(bc<<<"scale=2;$t*18/10-459.67") F"
echo "Rankine: $(bc<<<"scale=2;$t*18/10") R"
done

View file

@ -0,0 +1,24 @@
Option Explicit
Sub Main_Conv_Temp()
Dim K As Single, Result As Single
K = 21
Debug.Print "Input in Kelvin : " & Format(K, "0.00")
Debug.Print "Output in Celsius : " & IIf(ConvTemp(Result, K, "C"), Format(Result, "0.00"), False)
Debug.Print "Output in Fahrenheit : " & IIf(ConvTemp(Result, K, "F"), Format(Result, "0.00"), False)
Debug.Print "Output in Rankine : " & IIf(ConvTemp(Result, K, "R"), Format(Result, "0.00"), False)
Debug.Print "Output error : " & IIf(ConvTemp(Result, K, "T"), Format(Result, "0.00"), False)
End Sub
Function ConvTemp(sngReturn As Single, Kelv As Single, InWhat As String) As Boolean
Dim ratio As Single
ConvTemp = True
ratio = 9 / 5
Select Case UCase(InWhat)
Case "C": sngReturn = Kelv - 273.15
Case "F": sngReturn = (Kelv * ratio) - 459.67
Case "R": sngReturn = Kelv * ratio
Case Else: ConvTemp = False
End Select
End Function