본문 바로가기

프로그래밍 기술 노트/Functional Study

(30)
[Monad/Free Monad] 내멋대로 Free 모나드 이해하기 Free Monad + 인터프리터 패턴은 재대로 써본적이 없어서 맨날 까먹는다... 까먹지 않기 위해 포스팅 FP 에서는 순수를 지향하기 때문에... 부수효과가 섞이는것을 꺼린다, 특히 부수효과는 쉽게 전파되기 때문에 부수효과를 최대한 배재하고자 한다. 그런데 부수효과 없는 프로그램이란 없는법, 콘솔 출력도 해야되고 파일 저장도 해야되고 DB엑세스도 해야긋지.. 근데 그럼 어떻게 하느냐! 바로 [효과] 자체를 분리하고, [효과] 가 필요한 순간에만 [효과]를 적용시키는것! 엥 그거 완전 의존성 주입 아니냐? 인터페이스를 이용한 OOP 식 DI, ISP 가 깨질확률이 높고, Compose를 할수없고, Funciton 자체를 DI 받는경우는, 효과가 분리되야할 Function 이 너무 많으면 모든 Funct..
[Monad] 내멋대로 Monad 이해하기 - Bind (FlatMap), Monad C#이나 JAVA 에서 보통 함수를 어떻게 만들까? 예를 들어서 number를 받아서 User를 만드는 함수라고 하고한다면... public class User { public int id { get; set; } } public User CreateUser (int id) { return new User { id = id }; } 뭐 이런식으로 만들지 않을까? 그런데 여기서 User Id 가 0인 경우 관리자기 때문에 만들지 못한게 한다면? public class User { public int id { get; set; } } public User CreateUser (int id) { if (id == 0) { return null; } return new User { id = id }; } 뭐 간..
[Monad] 내멋대로 Monad 이해하기 - Map, Return, Apply FP 에서는 주로 예외처리를 Option 혹은 Maybe 라는 이름을 가진 타입을 사용한다. Null 처리 같은것도 Option으로 대체 되는데 예를 들자면 다음과 같다. MyClass nc = new MyClass(); //.... if (nc != null) { //... } else { //... } C#의 경우 위와 같이 Null로 확인한다면 let nt = Option.None //... if nt.IsSome then //... else //... F# 은 위와같이 Option을 통하여 None 인지 Some 인지 확인한다. Null이 천만불짜리 실수다~,Option 타입이 왜좋다~ 같은건 둘째 치고 단순위 위와같이 매번 if문 으로 Some 인지 None 인지 구분하여 사용한다면 Option..
[Functional Study] Poker Game https://github.com/Lee-WonJun/FP-Seminar/blob/master/Tutorial/Poker/clojure/poker.clj Lee-WonJun/FP-Seminar Contribute to Lee-WonJun/FP-Seminar development by creating an account on GitHub. github.com F# 으로 짠것을 clojure로 포팅. 그나저나 clojure에서 match 매크로를쓸때 vscode + clava 에서 문법틀렷다고 빨간줄이뜨는디.. IntelliJ + Cursive 로 넘어가야되나..
[Functional Study] Poker Game https://github.com/Lee-WonJun/FP-Seminar/blob/master/Tutorial/ConcentrationGame/F%23/Program-leewonjun.fs Lee-WonJun/FP-Seminar Contribute to Lee-WonJun/FP-Seminar development by creating an account on GitHub. github.com 포커로직을 F# 으로 구현 패턴매칭만 엄청쓴듯하다. 테스트를 Repl 로만 대충 해봐서 어딘가 잘못된 로직이 있을수도.. 원래는게임까지 구현하려고 턴마다 한장씩 나눠주는 것을 구현했는데 생각해보니까 5장 미리 나눠주고 UI 상에서만 안뿌리면 되는거 같아서.. 지웠다..
Clojure 에서 모나드는 필요없다? https://grishaev.me/en/no-monads/ I will never let monads be in a Clojure project Sounds a bit aggressive, doesn’t it? Well, let me explain. Monads are fine while you’re playing with them in Haskell. But don’t bring them to Clojure. They are completely foreign beings to Clojure ecosystem. Besides the technical issues, there is a problem grishaev.me Clojure에서도 monad 라이브러리가 있긴하지만.. 위 글에서는 하스켈에서 모나..
[Functional Study] ConcentrationGame https://github.com/Lee-WonJun/FP-Seminar/blob/master/Tutorial/ConcentrationGame/clojure/Program-leewonjun.clj Lee-WonJun/FP-Seminar Contribute to Lee-WonJun/FP-Seminar development by creating an account on GitHub. github.com 스터디용으로 신경쇠약 게임을 clojure 으로 구현해보았다. 기본적으로 F#으로 짠걸 베이스로 옮기면서 이것저것 수정했는데 일단 UI보다는 로직적인 부분을 구현했는데 생각해보니 F#으로 짤때 Game-loop를 도는 부분을 좀 이상하게 짠것 같아서 해당 부분의 로직을 변경하였다. F# 에서는 루프 하나에 한..
[Functional Study] ConcentrationGame https://github.com/Lee-WonJun/FP-Seminar/blob/master/Tutorial/ConcentrationGame/F%23/Program-leewonjun.fs Lee-WonJun/FP-Seminar Contribute to Lee-WonJun/FP-Seminar development by creating an account on GitHub. github.com 스터디용으로 신경쇠약 게임을 F# 으로 구현해보았다. 예외처리는 안되어있고.. 뭔가 FP 스럽지 않게 짠거같기도한데.. 흠