Last active
July 24, 2023 20:02
-
-
Save hellwolf/9fa906f4a72726cad7afdf68593098af to your computer and use it in GitHub Desktop.
Hypothetical hscript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env hsh | |
-- a. The file name should probably be ".hs", but it's nice to have all the syntax highlitng for free using ".hs". | |
-- b. it works by having a shell running as a separate process, while the hscript process piping stuff from these quasi-quote | |
-- template haskell outputs to the shell. | |
-- c. `hsh` preprocessor reduces some boiler plate, so it is extremely terse to start your helloworld program! | |
-- d. Otherwise it all looks your regular Haskell program, feel free to add 20+ more GHC extensions. | |
banner s = "hello world, " <> s | |
-- using https://hackage.haskell.org/package/PyF | |
[source| | |
echo {banner "mister"} | |
a=41 | |
|] | |
-- "source" works like bash source, it doesn't create new shell, so variables are shared | |
[source| | |
echo "variable is shared: $a" | |
|] | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env hsh | |
substring :: String -> String ->Bool | |
substring l s = check' l s True where | |
check' _ [] h = True | |
check' [] _ h = False | |
check' (x:xs) (y:ys) h = (y == x && check' xs ys False) || (h && check' xs (y:ys) h) | |
-- | This creates a pair of pipes (stdout, stderr) to the output of `dmesg -w` | |
[pipe12| | |
dmesg -w | |
|] (\(stdout, _) -> go | |
-- Only print messages that contains ACPI | |
where go = hGetLine stdin >>= (\l -> if substring l "ACPI" then putStrLn l else return ()) >> go | |
) | |
-- | In the same spirit, you may also pipe also the stdin. | |
[piped0| | |
-- ... I don't know, think of something | |
|] (\stdin -> | |
-- ... | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment