lytlang/README.md

91 lines
2.8 KiB
Markdown
Raw Permalink Normal View History

2019-05-11 13:01:39 -05:00
# Lytlang
2019-11-15 00:51:27 -06:00
Lytlang is an opinionated way to write more readable Elixir code.
2019-05-11 13:01:39 -05:00
2019-06-16 00:59:42 -05:00
```
2019-07-19 21:54:53 -05:00
!module Leetcode
2019-06-16 00:59:42 -05:00
# 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
2019-06-16 01:00:12 -05:00
[n1 | r1] [n2 | r2] l carry
2019-06-16 00:59:42 -05:00
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
2019-06-16 00:55:27 -05:00
2019-06-16 00:59:42 -05:00
TODO: better utility function names?
2019-05-11 13:01:39 -05:00
2019-06-16 00:59:42 -05:00
fn longest_non_repeating_substring s
s |> String.codepoints |> Enum.reduce {0 0 0 %{}} &ln3_reducer/2 |> elem 0
2019-05-11 13:01:39 -05:00
2019-06-16 00:59:42 -05:00
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
2019-05-11 13:01:39 -05:00
```
2019-06-16 00:59:42 -05:00
**TODO: Add more description**
2019-06-16 01:01:47 -05:00
**NOTE**: This doesn't work at all. I'm still figuring out how to even go about
2019-07-19 21:54:53 -05:00
creating an AST and converting that into valid Elixir code. Also, this will
probably never even happen.
2019-06-16 01:01:47 -05:00
2019-06-16 00:59:42 -05:00
## Development Resources
2019-11-19 23:38:36 -06:00
+ http://erlang.org/doc/man/yecc.html
+ http://erlang.org/doc/man/leex.html
2019-11-19 21:11:28 -06:00
+ 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
2019-06-16 00:59:42 -05:00
+ https://pl-rants.net/posts/leex-yecc-in-elixir/
+ https://notes.eellson.com/2017/01/22/html-parsing-in-elixir-with-leex-and-yecc/
2019-07-19 21:54:53 -05:00
+ https://github.com/elixir-lang/elixir/commits/master?after=e2c78e8ba948739768a335999e35a06d5eb5ad4b+15680
ish
+ https://github.com/elixir-lang/elixir/commit/b190523c6eb65a3660ba968106e2647170c1981c