this post was submitted on 01 Dec 2024
14 points (100.0% liked)

Concatenative Programming

152 readers
1 users here now

Hello!

This space is for sharing news, experiences, announcements, questions, showcases, etc. regarding concatenative programming concepts and tools.

We'll also take any programming described as:


From Wikipedia:

A concatenative programming language is a point-free computer programming language in which all expressions denote functions, and the juxtaposition of expressions denotes function composition. Concatenative programming replaces function application, which is common in other programming styles, with function composition as the default way to build subroutines.

For example, a sequence of operations in an applicative language like the following:

y = foo(x)
z = bar(y)
w = baz(z)

...is written in a concatenative language as a sequence of functions:

x foo bar baz


Active Languages

Let me know if I've got any of these misplaced!

Primarily Concatenative

Concatenative-ish, Chain-y, Pipe-y, Uniform Function Call Syntax, etc.


Cheat Sheets & Tutorials

Discord

IRC

Wikis

Wikipedia Topics

Subreddits

GitHub Topics

Blogs

Practice

founded 1 year ago
MODERATORS
14
submitted 2 days ago* (last edited 2 days ago) by Andy to c/concatenative
 

Alright, show me I'm not the only one in this community, and show off some solutions!

Here's my Day 1 solution in Factor (minus imports):

spoiler

: get-input ( -- left-list right-list )
  "aoc-2024.01" "input.txt" vocab-file-lines
  [ split-words harvest ] map unzip
  [ [ string>number ] map ] bi@ ;

: part1 ( -- n )
  get-input
  [ sort ] bi@
  [ - abs ] 2map-sum ;

: part2 ( -- n )
  get-input
  histogram
  '[ dup _ at 0 or * ] map-sum ;

Sadly, Factor doesn't get highlighted here, so here it is again as an image:

spoiler

syntax-highlighted screenshot of the code above

I probably won't last the week, but what solutions I do have will be up on GitHub.

top 4 comments
sorted by: hot top controversial new old
[–] Andy 2 points 2 hours ago

More Factor solutions for the first 3 days (at time of comment) from okflo, on sourcehut.

[–] Andy 1 points 22 hours ago

Some more Factor solutions for the first 3 days (so far) from soweli Niko, on Codeberg.

[–] Andy 1 points 1 day ago* (last edited 1 day ago)

Day 3

spoiler

: get-input ( -- corrupted-input )
  "aoc-2024.03" "input.txt" vocab-file-path utf8 file-contents ;

: get-muls ( corrupted-input -- instructions )
  R/ mul\(\d+,\d+\)/ all-matching-subseqs ;

: process-mul ( instruction -- n )
  R/ \d+/ all-matching-subseqs
  [ string>number ] map-product ;

: solve ( corrupted-input -- n )
  get-muls [ process-mul ] map-sum ;

: part1 ( -- n )
  get-input solve ;

: part2 ( -- n )
  get-input
  R/ don't\(\)(.|\n)*?do\(\)/ split concat
  R/ don't\(\)(.|\n)*/ "" re-replace
  solve ;

Image:

spoiler

code above as colorful image

[–] Andy 1 points 2 days ago* (last edited 2 days ago)

Day 2:

spoiler

: get-input ( -- reports )
  "aoc-2024.02" "input.txt" vocab-file-lines
  [ split-words [ string>number ] map ] map ;

: slanted? ( report -- ? )
  { [ [ > ] monotonic? ] [ [ < ] monotonic? ] } || ;

: gradual? ( report -- ? )
  [ - abs 1 3 between? ] monotonic? ;

: safe? ( report -- ? )
  { [ slanted? ] [ gradual? ] } && ;

: part1 ( -- n )
  get-input [ safe? ] count ;

: fuzzy-reports ( report -- reports )
  dup length <iota> [ remove-nth-of ] with map ;

: tolerable? ( report -- ? )
  { [ safe? ] [ fuzzy-reports [ safe? ] any? ] } || ;

: part2 ( -- n )
  get-input [ tolerable? ] count ;

Image:

spoiler

image of above code