<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Jyoti Prakash on Medium]]></title>
        <description><![CDATA[Stories by Jyoti Prakash on Medium]]></description>
        <link>https://medium.com/@jyotiprakash_47924?source=rss-1b4845eb3fee------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*taG22RRejdWPI-8O.</url>
            <title>Stories by Jyoti Prakash on Medium</title>
            <link>https://medium.com/@jyotiprakash_47924?source=rss-1b4845eb3fee------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 09:49:34 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jyotiprakash_47924/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Lisp styled lists in Rust]]></title>
            <link>https://medium.com/@jyotiprakash_47924/lisp-styled-lists-in-rust-735648bed319?source=rss-1b4845eb3fee------2</link>
            <guid isPermaLink="false">https://medium.com/p/735648bed319</guid>
            <category><![CDATA[rust]]></category>
            <category><![CDATA[rust-programming-language]]></category>
            <dc:creator><![CDATA[Jyoti Prakash]]></dc:creator>
            <pubDate>Sat, 03 Dec 2022 15:48:27 GMT</pubDate>
            <atom:updated>2022-12-03T15:48:27.968Z</atom:updated>
            <content:encoded><![CDATA[<p>Rust is rising in popularity among system programmers owing to his unique ownership and borrowing rules which eliminates many memory related bugs. In this article, we will see a nice academic exercise, implementing lisp style (or functional style) list in Rust. The purpose of this article is to introduce referential transparency, which allow developer to have cleaner abstraction that are easy to debug and manage. The list created in this article is not aimed at performance.</p><p>To start with, we first need to define a datatype of List. A list is defined as either having a “nil” element or an element concatenated with the list.</p><blockquote><strong>List = Nil | Cons(element, List)</strong></blockquote><p>So, we start with defining an enum containing two elements, Nil and Cons(i32, Box&lt;List&gt;). Box&lt;List&gt; is equivalent to pointer. Before going further, let us recall that Rust allocates all values on stack by default. To do this, it has to determine the length of the datatype before allocating memory on stack. This is fairly simple for primitive datatypes or structs or enum whose sizes are known beforehand.</p><pre>#[derive(Debug, Clone)]<br>enum List {<br>    Cons(i32, Box&lt;List&gt;),<br>    Nil,<br>}</pre><p>However, in this case, the scenario is a little bit peculiar. As Rust tries to infer the type of List during compile time, Cons(i32, List) throws the compiler in a <em>catch-22</em> situtation where it tries determine the type of List which it is still determining. Therefore, Rust doesn&#39;t allow this syntax and the only way out is let the compiler know to allocate it on heap. It is possible through Box [1]. This is quite similar to the declaration in languages such as C and Java, as opposed to functional languages, where one can directly take the mathematical definition such as List a = Nil | Cons a (List a) in Haskell.</p><p>Now, we have defined the type and proceed to the implemenation. In rust, you can encapuslate the implementation in the impl List { ... function ...} construct. The remaining of the functions have been defined inside this construct.</p><p>To create a new list, we can define, a method to create new List with Nil element.</p><pre>fn new() -&gt; Self {<br>  Self::Nil<br>}</pre><p>Next, we define a function append to append an element x to the list.</p><pre>fn append(self, x: i32) -&gt; List {<br>  match self {<br>    Self::Cons(t, l1) =&gt; {<br>        let t1 = l1.append(x); // recursively calls append on the part of the list<br>        Cons(t, Box::new(t1))<br>    }<br>    Self::Nil =&gt; {<br>        Cons(x, Box::new(Nil))<br>    }<br>  }<br>}</pre><p>Next, we define a function append to add an element x to the end of the list. It recurses on sub-list l1 . In both cases, it wraps the new list in a Cons element and returns it. Contrary to the imperative style, it returns a new list after appending the element to the end of the list. In terms of programming language theory these lists are as <em>referentially transparent</em> [2].</p><p>Similar, it is also possible to define concat function that concatenates two lists. It matches on the list, if it founds a Nil element, then it contacts the l2 to the list. Otherwise, it recurses to the Nil element in the first list and concatenates. It returns the list by wrapping the new list with Cons.</p><pre>fn concat(self, l2: List) -&gt; List {<br>  match self {<br>    Self::Nil =&gt; l2,<br>    Self::Cons(t, t1) =&gt; {<br>        let new_list = t1.concat(l2);<br>        Self::Cons(t, Box::new(new_list))<br>    }<br>  }<br>}</pre><p>To search for an element on the list, you can “unwrap” the list until you reach the element you want to search or reach the end of the list. It matches on self , in case it is Cons(v, l) then it check for element in v otherwise, it searches in l. It terminates when it reaches the end of the list.</p><pre>fn find(self, x: i32) -&gt; bool {<br>    match self {<br>        Self::Cons(v, l) =&gt; if v == x {true} else {l.find(x)},<br>        Self::Nil =&gt; false<br>    }<br>}</pre><p>Writing the head function is straightforward</p><pre>fn head(self) -&gt; Option&lt;i32&gt; {<br>   match self </pre><p><strong>Referential Transparency</strong></p><p>Referential transparency means that the functions has no side-effects and only performs pure computation. By virtue of this property, one can replace the expressions by equivalent expression without changing the semantics.</p><p>[1] <a href="https://doc.rust-lang.org/rust-by-example/std/box.html">https://doc.rust-lang.org/rust-by-example/std/box.html</a></p><p>[2] <a href="https://en.wikipedia.org/wiki/Referential_transparency">https://en.wikipedia.org/wiki/Referential_transparency</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=735648bed319" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[C++ Lambda Function: Under the hood]]></title>
            <link>https://medium.com/@jyotiprakash_47924/c-lambda-function-under-the-hood-dd417ed2acf?source=rss-1b4845eb3fee------2</link>
            <guid isPermaLink="false">https://medium.com/p/dd417ed2acf</guid>
            <category><![CDATA[cpp17]]></category>
            <category><![CDATA[cpp]]></category>
            <category><![CDATA[cpplus]]></category>
            <dc:creator><![CDATA[Jyoti Prakash]]></dc:creator>
            <pubDate>Sat, 29 Oct 2022 22:14:54 GMT</pubDate>
            <atom:updated>2022-11-09T20:45:38.590Z</atom:updated>
            <content:encoded><![CDATA[<p>Lambda functions are a fantastic feature of modern C++. Inspired from functional programming languages (e.g. Lisp, Haskell, OCaml, etc.), this feature has found its way into imperative programming languages such as C++ and Java. Lambda functions are helpful in keeping the code clean by getting rid of single-use functions/functors. At the outset, lambda functions might look like function pointers, but these are way beyond function pointers.</p><p>In principle, lambda in C++ are simple functors under the hood. If you don’t remember, functors are overloaded `operator()`. It allows you to call a class object as if it were a function. We will see some of the properties of lambda through the lens of functors. With this understanding, one can recognise the principles behind the properties of lambda functions.</p><p>Let us start with a simple program, where we define the generic lambda function doubleVal, which doubles the value of argument. It calls function doubleVal on line 4, like any other function call.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3b5ea8d903488fa99491996995fed7e8/href">https://medium.com/media/3b5ea8d903488fa99491996995fed7e8/href</a></iframe><p>To implement this feature, the compiler generates the class __doubleVal and overloads with operator() at line 5. Since, doubleVal is defined for the type auto, it creates a functor template with the type LType. The body of functor is copied from body of lambda function. Like any other templatized class definition, it can instantiate the template based on the datatype inferred at line 4. In this case, it uses a default constructors, as there are no captured variables.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/324f2b1ee95a9cfe991af44ca32a7e79/href">https://medium.com/media/324f2b1ee95a9cfe991af44ca32a7e79/href</a></iframe><p>Let us introduce capture variables here. The lambda variables in the next example captures the local variables defined in the main function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/59d3e54c7f28bed50e61fcefb5b55352/href">https://medium.com/media/59d3e54c7f28bed50e61fcefb5b55352/href</a></iframe><p>Compiler creates a class definition for the lambda function with the captured variables as member variables. The member variables are initialised via the constructor by <em>copy by value</em>, if the lambda captures by value (=); or <em>copying by reference</em>, if the lambda captures by reference.</p><p>In the following example, the variable y is <em>captured by value</em> in the constructor. Note that the field at line 14 is a copy of the captured variable which hides the effect of y=y+1 in the outer scope. This is because, the lambda has <em>mutable</em> keyword. In other case, it would have created a <em>const-copy</em>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/00d19b2f55956fea41b474740bb07c1c/href">https://medium.com/media/00d19b2f55956fea41b474740bb07c1c/href</a></iframe><p>Now, let us come to the last case of capture by reference, such as</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a1d6c4c8ac6282a8738d9f9479cc8f75/href">https://medium.com/media/a1d6c4c8ac6282a8738d9f9479cc8f75/href</a></iframe><p>Here, the generated code varies slightly from the previous. Instead of normal field variable int y, it rather, generates a reference variable int &amp;y at line 14 in __doubleAddV. Therefore, the effect of y+1 also visible in the outer scope.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b1c17949f3b6ac7afb29981b77464247/href">https://medium.com/media/b1c17949f3b6ac7afb29981b77464247/href</a></iframe><p>Thus, lambda functions are class definitions under the hood. To me, it is an interesting design in retrofitting new language features in existing compiler implementations.</p><p><strong>References</strong></p><ol><li>Jeremiah Willcock, Jaakko Jarvi, Doug Gregor, Bjarne Stroustrup, Andrew Lumsdaine, “Lambda expressions and closures in C++” [<a href="https://stroustrup.com/N1968-lambda-expressions.pdf">https://stroustrup.com/N1968-lambda-expressions.pdf</a>]</li><li><a href="https://pages.cs.wisc.edu/~gerald/cs368/resources/Lambda.pdf">https://pages.cs.wisc.edu/~gerald/cs368/resources/Lambda.pdf</a></li><li>C++ Insights (<a href="https://cppinsights.io/">https://cppinsights.io/</a>) was quite helpful while preparing the article. It simplified the output from Clang AST Dump.</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dd417ed2acf" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>