Examples
========


Calculating Pi with Monte Carlo Method
--------------------------------------

Pseudo-random number generator with :py:class:`~hymn.types.state.State` monad:

.. literalinclude:: ../examples/monte_carlo.hy
  :language: clojure
  :lines: 7-

Example output:

.. code-block:: sh

  $ ./monte_carlo.hy 50000
  the estimate for pi = 3.14232


Calculating Sum
---------------

Wicked sum function with :py:class:`~hymn.types.writer.Writer` monad:

.. literalinclude:: ../examples/sum.hy
  :language: clojure
  :lines: 7-

Example output:

.. code-block:: sh

  $ ./sum.hy 123 456 789
  sum: 1368


The FizzBuzz Test
-----------------

The possibly over-engineered FizzBuzz solution:

.. literalinclude:: ../examples/fizzbuzz.hy
  :language: clojure
  :lines: 7-

Example output:

.. code-block:: sh

  $ ./fizzbuzz.hy 100
  1       2       fizz    4       buzz
  fizz    7       8       fizz    buzz
  11      fizz    13      14      fizzbuzz
  16      17      fizz    19      buzz
  fizz    22      23      fizz    buzz
  26      fizz    28      29      fizzbuzz
  31      32      fizz    34      buzz
  fizz    37      38      fizz    buzz
  41      fizz    43      44      fizzbuzz
  46      47      fizz    49      buzz
  fizz    52      53      fizz    buzz
  56      fizz    58      59      fizzbuzz
  61      62      fizz    64      buzz
  fizz    67      68      fizz    buzz
  71      fizz    73      74      fizzbuzz
  76      77      fizz    79      buzz
  fizz    82      83      fizz    buzz
  86      fizz    88      89      fizzbuzz
  91      92      fizz    94      buzz
  fizz    97      98      fizz    buzz


Interactive Greeting
--------------------

Greeting from :py:class:`~hymn.types.continuation.Continuation` monad:

.. literalinclude:: ../examples/greeting.hy
  :language: clojure
  :lines: 7-

Example output:

.. code-block:: sh

  $ ./greeting.hy
  Hi, what is your name?
  Please tell me your name!
  $ ./greeting.hy
  Hi, what is your name? Marvin
  Welcome, Marvin!


Greatest Common Divisor
-----------------------

Logging with :py:class:`~hymn.types.writer.Writer` monad:

.. literalinclude:: ../examples/gcd.hy
  :language: clojure
  :lines: 7-

Example output:

.. code-block:: sh

  $ ./gcd.hy 24680 1352
  calculating the greatest common divisor of 24680 and 1352
  24680 mod 1352 = 344
  1352 mod 344 = 320
  344 mod 320 = 24
  320 mod 24 = 8
  24 mod 8 = 0
  the result is: 8


Project Euler Problem 9
-----------------------

Solving problem 9 with :py:class:`~hymn.types.list.List` monad

.. literalinclude:: ../examples/euler9.hy
  :language: clojure
  :lines: 7-

Example output:

.. code-block:: sh

  $ ./euler9.hy
  Project Euler Problem 9 - list monad example
  https://projecteuler.net/problem=9
  There exists exactly one Pythagorean triplet
  for which a + b + c = 1000.  Find the product abc.
  31875000


Project Euler Problem 29
------------------------

Solving problem 29 with :py:func:`~hymn.operations.lift` and
:py:class:`~hymn.types.list.List` monad

.. literalinclude:: ../examples/euler29.hy
  :language: clojure
  :lines: 7-

Example output:

.. code-block:: sh

  $ ./euler29.hy
  Project Euler Problem 29 - lift and list monad example
  https://projecteuler.net/problem=29
  How many distinct terms are in the sequence generated by
  a to the power of b for 2 <= a <= 100 and 2 <= b <= 100?
  9183


Solving 24 Game
---------------

Nondeterministic computation with :py:class:`~hymn.types.list.List` monad and
error handling with :py:class:`~hymn.types.maybe.Maybe` monad:

.. literalinclude:: ../examples/solve24.hy
  :language: clojure
  :lines: 7-

Example output:

.. code-block:: sh

  $ ./solve24.hy 2 3 8 8
  ((2 * 8) - 8) * 3
  (3 / 2) * (8 + 8)
  3 / (2 / (8 + 8))
  ((8 - 2) - 3) * 8
  ((8 * 2) - 8) * 3
  ((8 - 3) - 2) * 8
  8 * (8 - (2 + 3))
  ((8 + 8) / 2) * 3
  (8 + 8) / (2 / 3)
  (8 + 8) * (3 / 2)
  8 * (8 - (3 + 2))
  ((8 + 8) * 3) / 2
