MyNixOS website logo
Description

Simple way to parse strings with Python-like format strings.

Please refer to the README file that ships with this package.

parse

Haskell, while notorious for its extremely powerful parser combinators library, lacks a quick-and-dirty way to parse simple-formatted strings in a one-liner. This is especially frustrating for Advent of Code, where the input to some puzzles, which is in other languages fairly painless to parse, requires some overly convoluted, hacky and noncompact solution.

Envious of Python's parse package, I decided to write my own, very stripped down version, in Haskell. This package provides a way to create a sort of template for how strings should look, and then parse them accodringly. While inspired by Python format strings, the type of each field is not specified in the string itself, and as a matter of fact, if the compiler can't guess them by their use, they must be specified explicitly (like in the example.)

Examples:

parse "It's {}, I love it!" "It's spam, I love it!" :: String
 == "spam"

parse "{} + {} = {}" "2.1568743 + 7.196057 = 9.3529313" :: (String, String, String)
 == ("2.1568743","7.196057","9.3529313")

A safe variant of parse is also provided, that returns Maybe instead of throwing an error:

parse "{} * {} = {}" "2 + 3 = 5" :: (String, String, String)
*** Exception: No candidates for "" in "2 + 3 = 5".

-- Whereas:
parseMaybe "{} * {} = {}" "2 + 3 = 5" :: Maybe (String, String, String)
 == Nothing

You can also parse lists quite easily:

map read $ parseList "{}, {}, {}, {}, {}, {}, {}, {}, {}, {}" "1, 2, 3, 5, 7, 11, 13, 17, 19, 23" :: [Int]
 == [1,2,3,5,7,11,13,17,19,23]

(Note: You're still parsing a fixed amount of values, but now you can, as shown in the example, map stuff to it.)

It should be noted that this package lacks most of the features supplied by parse, instead providing only basic parsing functionality. This may or may not change in the future.

Metadata

Version

0.1.0.0

Platforms (77)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows