yamlscript

2024-05-25T05:17:29.210329Z

I wrote a small snippet that uses an external library, can you code review it? https://github.com/danielmartincraig/yamlscript-experiments/blob/master/reverse.ys

2024-05-25T05:18:05.336099Z

How can I avoid using the parenthesis?

Markus Agwin 2024-05-25T09:25:17.691959Z

This worked for me:

defn main():
    result =:
      str/reverse: "hello world"
    say: result

2024-05-25T16:26:16.874999Z

Thanks!

Ingy döt Net 2024-05-25T22:11:56.314799Z

You need to use :: instead of . in namespaces.

$ ys -ce 'require clojure::string: => str'
(require '[clojure.string :as str])

Ingy döt Net 2024-05-25T22:12:59.246039Z

. is the chaining operator and there was no good way to keep it as a namespace separator.

Ingy döt Net 2024-05-25T22:14:02.010949Z

At some point I'll try to recognize when people make that mistake. (I do it all the time)

Ingy döt Net 2024-05-25T22:19:58.364049Z

but in YS you don't need to require clojure.string or alias as str. those are automatically available.

Ingy döt Net 2024-05-25T22:20:14.657379Z

!yamlscript/v0

require clojure::string: => foo 

say: foo/reverse("hello world")
say: str/reverse("hello world")
say: clojure::string/reverse("hello world")
all work

Ingy döt Net 2024-05-25T22:24:43.223909Z

!yamlscript/v0

defn main(name='world'):
  result =: str/reverse("hello $name")
  say: result
is a variation using default args and string interpolation

Ingy döt Net 2024-05-25T22:38:17.883149Z

The main function is automatically called with (apply main ARGS) ARGS is a "parsed" version of *command-line-args*

Ingy döt Net 2024-05-25T22:39:20.904179Z

$ cat foo.ys 
!yamlscript/v0

defn main(& args):
  say: json/dump(args)
  say: json/dump(ARGS)
  say: json/dump(ARGV)
  say: ns-name(NS)
  say: FILE
  each [a ARGS]:
    say: "$a -> $(type(a))"
$ ys foo.ys 'foo bar' 42 3.1415
["foo bar",42,3.1415]
["foo bar",42,3.1415]
["foo bar","42","3.1415"]
main
/home/ingy/foo.ys
foo bar -> class java.lang.String
42 -> class java.lang.Long
3.1415 -> class java.lang.Double
$ ys foo.ys -c
(defn
 main
 [& args]
 (say (json/dump args))
 (say (json/dump ARGS))
 (say (json/dump ARGV))
 (say (ns-name *ns*))
 (say FILE)
 (each [a ARGS] (say (str a " -> " (type a)))))
(apply main ARGS)