lytlang/README.md

91 lines
2.8 KiB
Markdown

# Lytlang
Lytlang is an opinionated way to write more readable Elixir code.
```
!module Leetcode
# Look, a comment!
### part of the comment
Look, a multiline comment!
part of the comment ###
@doc
LeetCode #1: two_sum
TODO: store a map of complements (differences) as iterating for O(n)
fn two_sum _:list _:int i=0 ::
[] _ _ -> nil
[cur | rest] target current_index
Enum.find_index rest, fn x -> cur + x == target ::
nil -> two_sum rest target current_index
i -> [current_index, 1 + current_index + i]
@doc LeetCode #2: add_two_numbers
fn add_two_numbers _:list _:list _=[] i=0 ::
[] _ l m, _ [] l m -> finish_add_two_numbers l m
[n1 | r1] [n2 | r2] l carry
add_two_numbers r1 r2 [rem (carry + n1 + n2) 10) | l] (div n1 + n2 10)
fn finish_add_two_numbers l:list m:int -> m == 0 ? l : [m | l]
@doc
LeetCode #3: longest non-repeating susbtring
TODO: better utility function names?
fn longest_non_repeating_substring s
s |> String.codepoints |> Enum.reduce {0 0 0 %{}} &ln3_reducer/2 |> elem 0
fn ln3_new_least least map char
Map.has_key? map char ::
true -> max least ((Map.get map char) + 1)
_ -> least
fn ln3_reducer char {mx i least map}
least = ln3_new_least least map char
{(max mx (i - least + 1) (i + 1) least (Map.put map char i)}
@doc
LeetCode #4: median of sorted arrays
TODO: solution should me O(log(m+n))
TODO: finish an actual solution?
fn median_of_sorted_arrays _ _ -> 3
@doc LeetCode #65: Valid Number
@valid_decimal_number_regex ~r/^\s*(?:\+|-)?\d+(?:\.(?=\d))?\d*(?:e(?=[\d\+-]))?(?:\+|-)?\d*(?:\.(?=\d))?\s*$/
fn valid_decimal_number s -> Regex.match? @valid_decimal_number_regex s
```
**TODO: Add more description**
**NOTE**: This doesn't work at all. I'm still figuring out how to even go about
creating an AST and converting that into valid Elixir code. Also, this will
probably never even happen.
## Development Resources
+ http://erlang.org/doc/man/yecc.html
+ http://erlang.org/doc/man/leex.html
+ https://hexdocs.pm/elixir/syntax-reference.html#the-elixir-ast
+ https://medium.com/cirru-project/resources-on-elixir-ast-e045b613f284
+ https://elixir-lang.org/getting-started/meta/quote-and-unquote.html
+ https://hexdocs.pm/elixir/Kernel.SpecialForms.html
+ http://slides.com/chrismccord/elixir-macros
+ http://zhenwusw.github.io/blog/2014/10/20/metaprogramming
+ https://www.theerlangelist.com/2014/06/understanding-elixir-macros-part-1.html
+ https://www.theerlangelist.com/2014/06/understanding-elixir-macros-part-3.html
+ https://pragprog.com/book/cmelixir/metaprogramming-elixir
+ https://pl-rants.net/posts/leex-yecc-in-elixir/
+ https://notes.eellson.com/2017/01/22/html-parsing-in-elixir-with-leex-and-yecc/
+ https://github.com/elixir-lang/elixir/commits/master?after=e2c78e8ba948739768a335999e35a06d5eb5ad4b+15680
ish
+ https://github.com/elixir-lang/elixir/commit/b190523c6eb65a3660ba968106e2647170c1981c