Skip to content
/ advent Public

Simple functional Python library designed for learning and problem solving purposes inspired by APL and Elixir programming languages

License

Notifications You must be signed in to change notification settings

MetGang/advent

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

advent

Simple functional Python library designed for learning and problem solving purposes inspired by APL and Elixir programming languages.

Created mainly for solving Advent of Code challenges - hence the name advent.

Motivation

While Python is a very versatile and powerful programming language it lacks functional approach found in other languages.

For better demonstration let's look at the puzzle Calorie Counting from day 1 of Advent of Code 2022.

Regular Python with loops
with open('input.txt') as file:
    content = file.read()
    biggest = 0
    for chunk in content.split('\n\n'):
        total = 0
        for line in chunk.split('\n'):
            n = int(line)
            total += n
        if total > biggest:
            biggest = total
    print(biggest)
More functional oriented Python
with open('input.txt') as file:
    content = file.read()
    parsed = (map(int, line.split('\n')) for line in content.split('\n\n'))
    solution = max(map(sum, parsed))
    print(solution)
Python with advent
from advent import *

f = read_file('input.txt') | txt.split('\n\n') | map(txt.split('\n') | map(int) | sum()) | max()

print(f())
Elixir solution for comparision
File.read!("input.txt")
  |> String.split("\n\n")
  |> Stream.map(fn line ->
    line
    |> String.split("\n")
    |> Stream.map(&String.to_integer/1)
    |> Enum.sum()
  end)xD
  |> Enum.max()
  |> IO.puts()

Modules

  • algorithms
  • combinators
  • functions
  • generators
  • operators (op)
  • regex (rgx)
  • textual (txt)

Features

  • Clean interface (arguably)
  • Functions piping
  • Arguments binding
  • Basic combinators

References

About

Simple functional Python library designed for learning and problem solving purposes inspired by APL and Elixir programming languages

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages