DEV Community

vindarel
vindarel

Posted on • Edited on

1 1 1

Advent of Code: alexandria's map-permutations was perfect for day 08. Common Lisp tip.

As the title says. Did you know about alexandria:map-permutations ?

If not, you could have discovered it with (apropos "permutation"). Run this on the REPL, it returns you all results it knows of symbols that have it in their name. You can also try apropos-regex from cl-ppcre. It's best to run them when you have loaded a few utility packages, like Alexandria or Serapeum. I run them when I loaded CIEL, so they are here, plus a few more.

Look at the Alexandria functions on sequences here:

map-permutations:

Calls function with each permutation of length constructable from
the subsequence of sequence delimited by start and end. start
defaults to 0, end to length of the sequence, and length to the
length of the delimited subsequence.
Enter fullscreen mode Exit fullscreen mode

If you call it on a large input, it won't return an insanely large list of permutations. We can work on them one after the other. You can also pass :copy nil to it, in which case "all combinations are eq to each other", so I suppose the function re-uses a structure, and it is advised to not modify the permutation.

Examples:

(alexandria:map-permutations (lambda (it) (print it)) '(a b))
;; or just
(alexandria:map-permutations #'print '(a b))

(A B)
(B A)


(alexandria:map-permutations (lambda (it) (print it)) '(a b c))

(A B C)
(B A C)
(C A B)
(A C B)
(B C A)
(C B A)
Enter fullscreen mode Exit fullscreen mode

ask for permutations of length 2:

(alexandria:map-permutations (lambda (it) (print it)) '(a b c) :length 2)

(A B)
(B A)
(A C)
(C A)
(B C)
(C B)
Enter fullscreen mode Exit fullscreen mode

There is also map-combinations, this one considers (a b) and (b a) to be the same combination:

(alexandria:map-combinations (lambda (it) (print it)) '(a b c) :length 2)

(A B)
(A C)
(B C)
Enter fullscreen mode Exit fullscreen mode

I solved Advent of Code's day 08 with this. This year I learned ✓

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

Top comments (2)

Collapse
 
anquegi_91 profile image
Antonio Juan Querol

Thanks for this tip, why you do not change (lambda (it) (print it)) with #'print??

Collapse
 
vindarel profile image
vindarel

You are so right^^ My real code is doing more stuff in the lambda but this needs editing. Thanks.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay