{"action":"created","issue":{"url":"https://api.github.com/repos/SodiumFRP/sodium/issues/64","labels_url":"https://api.github.com/repos/SodiumFRP/sodium/issues/64/labels{/name}","comments_url":"https://api.github.com/repos/SodiumFRP/sodium/issues/64/comments","events_url":"https://api.github.com/repos/SodiumFRP/sodium/issues/64/events","html_url":"https://github.com/SodiumFRP/sodium/issues/64","id":94856263,"number":64,"title":"Java: flatMap","user":{"login":"romansl","id":378682,"avatar_url":"https://avatars.githubusercontent.com/u/378682?v=3","gravatar_id":"","url":"https://api.github.com/users/romansl","html_url":"https://github.com/romansl","followers_url":"https://api.github.com/users/romansl/followers","following_url":"https://api.github.com/users/romansl/following{/other_user}","gists_url":"https://api.github.com/users/romansl/gists{/gist_id}","starred_url":"https://api.github.com/users/romansl/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/romansl/subscriptions","organizations_url":"https://api.github.com/users/romansl/orgs","repos_url":"https://api.github.com/users/romansl/repos","events_url":"https://api.github.com/users/romansl/events{/privacy}","received_events_url":"https://api.github.com/users/romansl/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/SodiumFRP/sodium/labels/question","name":"question","color":"cc317c"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":15,"created_at":"2015-07-14T02:55:48Z","updated_at":"2015-07-17T04:58:48Z","closed_at":null,"body":"Library lacks primitive `flatMap`. It has strong practical usage. But I dont know how to implement it in terms of FRP.\r\n\r\nExample:\r\n```kotlin\r\npublic fun request(): Stream<String> {\r\n    // perform http request.\r\n}\r\n\r\npublic val refresh: StreamSink<Unit> = Sodium.streamSink<Unit>()\r\npublic val valueFromServer: Cell<String> = refresh.flatMap { request() }.hold(\"\")\r\n\r\npublic fun <A, B> Stream<A>.flatMap(transform: (Event<A>) -> Stream<B>): Stream<B> {\r\n    // how to implement?\r\n}\r\n```\r\n\r\nOr is there a way to do it without `flatMap`?"},"comment":{"url":"https://api.github.com/repos/SodiumFRP/sodium/issues/comments/122176122","html_url":"https://github.com/SodiumFRP/sodium/issues/64#issuecomment-122176122","issue_url":"https://api.github.com/repos/SodiumFRP/sodium/issues/64","id":122176122,"user":{"login":"the-real-blackh","id":738712,"avatar_url":"https://avatars.githubusercontent.com/u/738712?v=3","gravatar_id":"","url":"https://api.github.com/users/the-real-blackh","html_url":"https://github.com/the-real-blackh","followers_url":"https://api.github.com/users/the-real-blackh/followers","following_url":"https://api.github.com/users/the-real-blackh/following{/other_user}","gists_url":"https://api.github.com/users/the-real-blackh/gists{/gist_id}","starred_url":"https://api.github.com/users/the-real-blackh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/the-real-blackh/subscriptions","organizations_url":"https://api.github.com/users/the-real-blackh/orgs","repos_url":"https://api.github.com/users/the-real-blackh/repos","events_url":"https://api.github.com/users/the-real-blackh/events{/privacy}","received_events_url":"https://api.github.com/users/the-real-blackh/received_events","type":"User","site_admin":false},"created_at":"2015-07-17T04:58:48Z","updated_at":"2015-07-17T04:58:48Z","body":"I can't reproduce it in the Java version. I've written the test case below. Please add this to your test suite and see what happens. Check that the post() tasks are executed after the last() ones.\r\n\r\n```java\r\n\r\n    public void testSwitchAndDefer()\r\n    {\r\n        List<String> out = new ArrayList();\r\n        StreamSink<Integer> si = new StreamSink();\r\n        Listener l = Cell.switchS(si.map(i -> {\r\n            Cell<String> c = new Cell<>(\"A\"+i);\r\n            return Operational.value(c).defer();\r\n        }).hold(new Stream<String>())).listen(x -> { out.add(x); });\r\n        si.send(2);\r\n        si.send(4);\r\n        l.unlisten();\r\n        assertEquals(Arrays.asList(\"A2\", \"A4\"), out);\r\n    }\r\n```"}}