{"action":"closed","issue":{"url":"https://api.github.com/repos/michaelmacinnis/oh/issues/36","repository_url":"https://api.github.com/repos/michaelmacinnis/oh","labels_url":"https://api.github.com/repos/michaelmacinnis/oh/issues/36/labels{/name}","comments_url":"https://api.github.com/repos/michaelmacinnis/oh/issues/36/comments","events_url":"https://api.github.com/repos/michaelmacinnis/oh/issues/36/events","html_url":"https://github.com/michaelmacinnis/oh/issues/36","id":356603612,"node_id":"MDU6SXNzdWUzNTY2MDM2MTI=","number":36,"title":"Losing [my]self hacking oh :)","user":{"login":"cryptorick","id":349810,"node_id":"MDQ6VXNlcjM0OTgxMA==","avatar_url":"https://avatars1.githubusercontent.com/u/349810?v=4","gravatar_id":"","url":"https://api.github.com/users/cryptorick","html_url":"https://github.com/cryptorick","followers_url":"https://api.github.com/users/cryptorick/followers","following_url":"https://api.github.com/users/cryptorick/following{/other_user}","gists_url":"https://api.github.com/users/cryptorick/gists{/gist_id}","starred_url":"https://api.github.com/users/cryptorick/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cryptorick/subscriptions","organizations_url":"https://api.github.com/users/cryptorick/orgs","repos_url":"https://api.github.com/users/cryptorick/repos","events_url":"https://api.github.com/users/cryptorick/events{/privacy}","received_events_url":"https://api.github.com/users/cryptorick/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2018-09-03T21:46:47Z","updated_at":"2018-09-04T02:05:33Z","closed_at":"2018-09-04T02:05:33Z","author_association":"NONE","body":"Hi!  This all started when I wanted to reduce the noise of `... | while (define ln: readline) ...`; so, I created a function abstraction to help the cause.  (Yeah, yeah, I know: one person's noise is another person's music. :)\r\n```\r\n$ define mapl: method (f) = : while (define ln: readline): $f $ln\r\n```\r\nAnd in case `f` doesn't print/echo anything, but we want to use its value down the pipeline, there is a variant which prints `f`'s images.\r\n```\r\n$ define maplp: method (f) = : while (define ln: readline): echo: $f $ln\r\n```\r\nWorks look a charm!\r\n```\r\n$ echo rick michael | xargs -n1 | mapl: method (x) = : echo \"-->\" $x\r\n--> rick\r\n--> michael\r\n```\r\nThe more common use case for me is that such a function is already defined, as so.\r\n```\r\n$ define echo-with-arrow: method (:args) = : echo \"-->\" @$args\r\n```\r\nThen, the pipeline works the same as before -- no surprises.\r\n```\r\n$ echo rick michael | xargs -n1 | mapl $echo-with-arrow\r\n--> rick\r\n--> michael\r\n```\r\nNow, here's where things start to get interesting: I needed a little caching mechanism that spied on items coming down that pipeline and \"remembered\" \"seeing\" those items.  Here's what I used.\r\n```\r\n$ define things-seen: method () =: object {\r\n$     define what-seen: list\r\n$ \r\n$     export show: method self () = {\r\n$         echo $self::what-seen\r\n$     }\r\n$     export seen-this?: method self (this) = {\r\n$         member? $this $self::what-seen\r\n$     }\r\n$     export see-this: method self (this) = {\r\n$         if (not: seen-this? $this) {\r\n$             set $self::what-seen: cons $this $self::what-seen\r\n$         }\r\n$         return $this\r\n$     }\r\n$ }\r\n```\r\nOh yeah, you're gonna need this dependency too (list membership predicate).\r\n```\r\n$ define member: method (x xs) = {\r\n$     if (or (is-null $xs) (eq $x : xs::head)) {\r\n$         return $xs\r\n$     } else {\r\n$         member $x : xs::tail\r\n$     }\r\n$ }\r\n$ define member?: method (:args) = : boolean : member @$args\r\n```\r\nHere's a non-pipeline usage, to get an idea.\r\n```\r\n$ define seen: things-seen\r\n$ seen::show\r\n()\r\n$ seen::see-this \"rick\"\r\n$ seen::see-this \"michael\"\r\n$ seen::show\r\n\"michael\" \"rick\"\r\n```\r\nNow, when we stick it in the pipeline with `mapl`, we'll see some sparks flying. :)\r\n```\r\n$ echo rick michael | xargs -n1 | maplp $seen::see-this\r\nxargs: /bin/echo: terminated with signal 13; aborting\r\nissue-minimal-example.oh: 25: error/runtime: 'what-seen' undefined\r\n```\r\nOops.  It's as if we are losing track of that \"slot\" in the object -- actually, I think we've lost the `self` reference somehow, but that's just a guess.\r\n\r\nReplacing `mapl` with what it's supposed to do, all is right again.\r\n```\r\n$ echo rick michael | xargs -n1 | while (define ln: readline): echo: $seen::see-this $ln\r\nrick\r\nmichael\r\n```\r\n(BTW, I checked that the caching was also happening. :)\r\n\r\nSo, what am I doing wrong?\r\n\r\nHere's the code all in one place to plop into a file, for convenience.  Thanks! --Rick\r\n\r\n```\r\ndefine mapl: method (f) = : while (define ln: readline): $f $ln\r\n\r\ndefine maplp: method (f) = : while (define ln: readline): echo: $f $ln\r\n\r\ndefine member: method (x xs) = {\r\n    if (or (is-null $xs) (eq $x : xs::head)) {\r\n        return $xs\r\n    } else {\r\n        member $x : xs::tail\r\n    }\r\n}\r\ndefine member?: method (:args) = : boolean : member @$args\r\n\r\ndefine things-seen: method () =: object {\r\n    define what-seen: list\r\n\r\n    export show: method self () = {\r\n        echo $self::what-seen\r\n    }\r\n    export seen-this?: method self (this) = {\r\n        member? $this $self::what-seen\r\n    }\r\n    export see-this: method self (this) = {\r\n        if (not: seen-this? $this) {\r\n            set $self::what-seen: cons $this $self::what-seen\r\n        }\r\n        return $this\r\n    }\r\n}\r\n\r\ndefine echo-with-arrow: method (:args) = : echo \"-->\" @$args\r\n\r\n# These both give same output (of course).\r\n# $ echo rick michael | xargs -n1 | mapl: method (x) = : echo \"-->\" $x\r\n# $ echo rick michael | xargs -n1 | mapl $echo-with-arrow\r\n\r\n# Make an instance of things-seen (for use in the next two pipeline commands).\r\n# $ define seen: things-seen\r\n\r\n# This blows up.\r\n# $ echo rick michael | xargs -n1 | maplp $seen::see-this\r\n\r\n# This doesn't.\r\n# $ echo rick michael | xargs -n1 | while (define ln: readline): echo: $seen::see-this $ln\r\n\r\n```"}}
{"actor":{"display_login":"cryptorick"}}