DEV Community

PNS11
PNS11

Posted on • Edited on

4 1

First post, picolisp script

Today I made a oneliner for converting a set of ISO-coded XML files in the current directory to UTF-8.

(for X (dir) (and (match '(@A "." "x" "m" "l") (chop X))(and (out "sh.sh" (prinl "#! /bin/sh"))(out "+sh.sh" (prinl (pack "iconv -f ISO-8859-1 -t UTF-8 " X " > utf8_" X)))(call 'sh "sh.sh"]
Enter fullscreen mode Exit fullscreen mode

A more readable, commented version could look like this:

(for X (dir) # for every file name in this directory
    (and 
        (match '(@A "." "x" "m" "l") (chop X)) # check if *.xml
        (and # it is or this is skipped, so...
            (out "sh.sh" # we write a new file
                (prinl "#! /bin/sh")) # with a hashbang 
            (out "+sh.sh" # and then add an instruction
                (prinl 
                    (pack "iconv -f ISO-8859-1 -t UTF-8 " X " > utf8_" X)))
            (call 'sh "sh.sh"] # and finally run it 
Enter fullscreen mode Exit fullscreen mode

Now this isn't very lispy, but it can quickly be turned into a more functional style.

(de qconv (Lst) # '(FromCode ToCode OutPrefix InSuffix FileList) 
    (for X (last Lst)
        (and 
            (match '(@A ~(chop (cadddr Lst))) (chop X))
            (and 
                (out "sh.sh" 
                    (prinl "#! /bin/sh"))
                (out "+sh.sh" 
                    (prinl 
                        (pack "iconv -f " (car Lst) "-t " (cadr Lst) " " X " > " (caddr Lst) X)))
                )))
    (call 'rm "sh.sh"]
Enter fullscreen mode Exit fullscreen mode

Still quite ugly though.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay