unofficial mirror of guix-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: david larsson <david.larsson@selfhosted.xyz>
To: "(" <paren@disroot.org>
Cc: guix-devel@gnu.org,
	guix-devel-bounces+david.larsson=selfhosted.xyz@gnu.org
Subject: Re: Struggling to write Dissecting Guix, Part 2
Date: Sat, 28 Jan 2023 19:28:02 +0100	[thread overview]
Message-ID: <f01218eeffaa64096e3c20b59703a136@selfhosted.xyz> (raw)
In-Reply-To: <CQ1WVTASWD2W.7C1615FE0CQV@guix-framework>

[-- Attachment #1: Type: text/plain, Size: 855 bytes --]

On 2023-01-26 07:34, ( wrote:
> On Wed Jan 25, 2023 at 7:39 PM GMT, david larsson wrote:
>> https://towardsdatascience.com/monads-from-the-lens-of-imperative-programmer-af1ab8c8790c
> 
> I'm not too sure about this one, I'm afraid.
> 
>     -- (

What are you not too sure about?

There is also pseudo-code (so not python) version of it on Wikipedia 
that's almost identical: 
https://en.wikipedia.org/wiki/Monad_(functional_programming)#Program_logging

I thought the wikipedia version is kind of clearer mainly because it's a 
little more "complete"-feeling. I added some stuff myself to the first 
article making it more like the wikipedia, etc, the and attached it here 
if anyone's interested. That's still in python, but if I ever were to 
write a monad in guile, I'd almost certainly start with trying to 
translate this one.


Best regards,
David

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: mathlogsmonad-mini.py --]
[-- Type: text/x-python; name=mathlogsmonad-mini.py, Size: 1941 bytes --]

from typing import Tuple
def square(num: int) -> int:
    return num * num
def double(num: int) -> int:
    return num + num
def square_with_print_return(num: int) -> Tuple[int, str]:
    logs = "Currrent num " + str(num) + ". "
    return (square(num), logs)
def double_with_print_return(num: int) -> Tuple[int, str]:
    logs = "Currrent num " + str(num) + ". "
    return (double(num), logs)
def bind(func, tuple: Tuple[int, str]) -> Tuple[int, str]:
    res = func(tuple[0])
    return (res[0], tuple[1] + res[1])
def unit(number: int) -> Tuple[int, str]:
   return (number, "")

# Now you can do this, more or less nested:
#print(bind(square_with_print_return, unit(5)))
#print(bind(square_with_print_return, (bind(square_with_print_return, bind(square_with_print_return,bind(square_with_print_return,unit(5)))))))

# it's nicer with infix. This class is a hack.
class Infix:
    def __init__(self, function):
        self.function = function
    def __ror__(self, other):
        return Infix(lambda x, self=self, other=other: self.function(other, x))
    def __or__(self, other):
        return self.function(other)
    def __rlshift__(self, other):
        return Infix(lambda x, self=self, other=other: self.function(other, x))
    def __rshift__(self, other):
        return self.function(other)
    def __call__(self, value1, value2):
        return self.function(value1, value2)

x=Infix(lambda f,x: bind(f,x))
print( square_with_print_return |x| (double_with_print_return |x| unit(4) ) )

# And some extra functional stuff
def curry(f,x):
    def curried_function(*args, **kw):
        return f(*((x,)+args),**kw)
    return curried_function
curry=Infix(curry)

import operator
add5 = operator.add |curry| 5
print(add5(6))

def add5_with_print_return(num: int) -> Tuple[int,str]:
    logs = "Current num is " + str(num) + ". "
    return (5+num,logs)

print( add5_with_print_return |x| (double_with_print_return |x| unit(4) ) )

  reply	other threads:[~2023-01-28 18:28 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-25  7:12 Struggling to write Dissecting Guix, Part 2 (
2023-01-25  9:58 ` zimoun
2023-01-26  6:32   ` (
2023-01-25 15:54 ` Wojtek Kosior via Development of GNU Guix and the GNU System distribution.
2023-01-26  6:33   ` (
2023-01-26 11:17   ` Simon Tournier
2023-01-26 20:03     ` bokr
2023-01-27 11:50       ` Simon Tournier
2023-01-25 19:39 ` david larsson
2023-01-26  6:34   ` (
2023-01-28 18:28     ` david larsson [this message]
2023-01-26 20:45 ` Felix Lechner via Development of GNU Guix and the GNU System distribution.

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://guix.gnu.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=f01218eeffaa64096e3c20b59703a136@selfhosted.xyz \
    --to=david.larsson@selfhosted.xyz \
    --cc=guix-devel-bounces+david.larsson=selfhosted.xyz@gnu.org \
    --cc=guix-devel@gnu.org \
    --cc=paren@disroot.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).