<div dir="ltr"><div>When I have functions that are pure but depend on some common state(say in a config file, or retrieved from a database at startup), I like to use implicit parameters to hide it. You can use a type alias to avoid it cluttering up most signatures. Below, a value of type 'Environmental Float' means 'A float value, dependent on some fixed environment containing all students and the single unique classroom'. If you have a deep chain of 'Environmental a' values, the implicit parameter will be automatically propagated to the deepest parts of the expression.</div><div><br></div><div>You could also use a Reader monad, but they seem to require more invasive syntactic changes: They are better if you later expect to need other monads like IO, but if you're just doing calculations they're overkill. You could also define a type alias 'Environmental a = Environment -> a', but then if you have multiple such states they don't compose well(they require you to apply the implicit state in the correct order, and it can be a little awkward to propagate the parameter).</div><div><br></div><div>Here's how I would start to structure your example in a larger project:</div><div><br></div><div>{-# LANGUAGE ImplicitParams,RankNTypes #-}</div><div><br></div><div>import qualified Data.IntMap as M</div><div><br></div><div>newtype RowId a = RowId Int</div><div><br></div><div>data Classroom = Classroom { classroom_id :: RowId Classroom, classroom_extraFees :: Float, classroom_students :: [ RowId Student ] }</div><div>data Student = Student { student_id :: RowId Student, student_name::String, student_feesOwed::Float}</div><div><br></div><div>data Environment = Environment {</div><div>  environment_classroom :: Classroom,</div><div>  environment_students  :: M.IntMap Student</div><div>  }</div><div><br></div><div>type Environmental a = (?e :: Environment) => a</div><div><br></div><div>classroom :: (?e :: Environment) => Classroom</div><div>classroom = environment_classroom ?e</div><div><br></div><div>students :: (?e :: Environment) => M.IntMap Student</div><div>students = environment_students ?e</div><div><br></div><div>student_totalFeesOwed :: RowId Student -> Environmental Float</div><div>student_totalFeesOwed (RowId studentId) = classroom_extraFees classroom + (student_feesOwed $ students M.! studentId)</div><div><br></div><div>main = do</div><div>  let student = Student (RowId 1) "Bob" 250.00</div><div>  let ?e = Environment {</div><div>        environment_classroom = Classroom (RowId 1) 500.00 [ RowId 1 ],</div><div>        environment_students = M.fromList [ (1, student) ]</div><div>        }</div><div>  putStrLn $ show $ student_totalFeesOwed $ RowId 1</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Jul 5, 2016 at 6:26 PM, Guru Devanla <span dir="ltr"><<a href="mailto:gurudev.devanla@gmail.com" target="_blank">gurudev.devanla@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div><div>Hello All,<br><br></div>I am just getting myself to code in Haskell and would like to design advice.  Below, I have a made up example:<br>                                                                                                                                                                                                                                                            <br>                                                                                                                                                                                                                                                                                  <br>data ClassRoom = ClassRoom { classRoomNo:: Integer, extra_fees::Float, students: Map StudentId Student}<br>data Student = Student {name::String, feesOwed::Float}<br>data StudentId = Integer                                                                                                                                                                                                                                                          <br>                                                                                                                                                                                                                                                                                  <br>get_fees_owed classroom student_id = extra_fees + feesOwed $ (students classroom) M.! studentid                                                                                                                                                                        <br><br>Here the `get_fees_owed`  needs information from the container 'classroom'.  <br><br></div><div>Here is my question/problem:<br>                                                                                                   <br></div><div>I believe I should model most of my code as expressions, rather than storing pre-computed values such as `fees_owed`.  But, <br>defining expressions involve passing the container objects all over. For example, deep down in a function that deals with just<br></div><div>one `student`, I might need the fees owed information. Without, having a reference to the container, I cannot call get_fees_owed.<br><br></div><div>Also, I think it hinders composing of functions that just deal with one student at a time, but end up with some dependency on<br></div><div>the container.<br><br></div>I have several questions related to this design hurdle, but I will start with the one above.<br><br></div>Thanks!<span class="HOEnZb"><font color="#888888"><br></font></span></div><span class="HOEnZb"><font color="#888888">Guru<br><br><div><div><div><div><br></div></div></div></div></font></span></div>
<br>_______________________________________________<br>
Haskell-Cafe mailing list<br>
To (un)subscribe, modify options or view archives go to:<br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><br>
Only members subscribed via the mailman list are allowed to post.<br></blockquote></div><br></div>