alandipert / gherkin

a functional programming language and interpreter written in GNU Bash 4
https://tailrecursion.com/~alan/Lisp/GherkinHistory.html
BSD 3-Clause "New" or "Revised" License
522 stars 31 forks source link

Adding exec function #28

Closed adereth closed 11 years ago

adereth commented 11 years ago

I'm trying to port a few bash scripts to gherkin. I've got a few that exec at the end and I couldn't come up with a way of doing it besides adding a primitive function.

alandipert commented 11 years ago

Thanks! Interop is basically anything-goes until #23 is in, however with the existing sh! you can implement exec like this:

(def exec
  (fn (cmd)
    (sh! (str "exec " cmd))))

Also, per your earlier IRC message, and as of 20c70fb and cceee79, a function for matching regexes can be implemented like this:

(def re-matches?
  (fn (re s)
    (sh! (str "[[ '" s "' =~ " re " ]]"))))

(re-matches? "^he.*$" "hello") ;=> t
(re-matches? "^he.*$" "foo") ;=> nil
adereth commented 11 years ago

Thanks! It wasn't clear to me that I could just use bash syntax in the arguments to sh!... I'm just so conditioned to not being able to use even simple bashisms when creating commands to be construct a Process on the JVM that it didn't even occur to me.

alandipert commented 11 years ago

Glad to help. sh! maybe isn't properly named - it's closer to ClojureScript's js* in operation in that its argument is evaluated by bash and not necessarily shelled to.