Logo

dev-resources.site

for different kinds of informations.

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

Published at
12/9/2024
Categories
lisp
commonlisp
adventofcode
Author
vindarel
Categories
3 categories in total
lisp
open
commonlisp
open
adventofcode
open
Author
8 person written this
vindarel
open
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 โœ“

lisp Article's
30 articles in total
Favicon
Common Lisp HTML templates: using Djula in .lisp files
Favicon
Remaking a rule-engine DSL
Favicon
Advent of Code: alexandria's map-permutations was perfect for day 08. Common Lisp tip.
Favicon
C and C++ are really so fast?
Favicon
Scheming About Clojure
Favicon
FTP and SFTP clients for Common Lisp
Favicon
Common Lisp with batteries included: CIEL v0.2 (aka fast scripting with useful libraries)
Favicon
A shell script that I usually run after installing SBCL
Favicon
Common Lisp VS Haskell
Favicon
Debugging Common Lisp: "Iย feel so much faster and free"
Favicon
Learning Lisp - making sense of xrefs in SLIME
Favicon
Learning Lisp
Favicon
Common Lisp's groupBy is Serapeum:assort
Favicon
Common Lisp VS C: a testimony
Favicon
Lisp: simplifying my problems through code
Favicon
Python VS Common Lisp applied: print, log and icecream
Favicon
Lisp-style list in Rust
Favicon
RainLisp on .NET
Favicon
Is Elixir or Common Lisp the best language for building a bootstrapped B2B SaaS in 2024?
Favicon
Form validation in Common Lisp
Favicon
Common Lisp GUI with Electron ยท quick how to
Favicon
Throttle/debounce a Common Lisp function
Favicon
OOP via FP : functional nature of classes and objects
Favicon
5 ways to get text from an Emacs buffer
Favicon
Configuring Neovim with Fennel
Favicon
Coding in the Shadows: Hidden Gems of Lisp, Clojure, and Friends
Favicon
Emacs is More Like a Terminal Than an Editor
Favicon
Do you need an IDE to write Common Lisp?
Favicon
Entendendo Algoritmos - Cap.5 Quicksort em Clojure
Favicon
The unreasonable effectiveness of working with a live programming image

Featured ones: