<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Chrisichris&#039;s Blog</title>
	<atom:link href="http://chrisichris.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://chrisichris.wordpress.com</link>
	<description>JProgrammingFun</description>
	<lastBuildDate>Thu, 08 Dec 2011 11:59:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='chrisichris.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Chrisichris&#039;s Blog</title>
		<link>http://chrisichris.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://chrisichris.wordpress.com/osd.xml" title="Chrisichris&#039;s Blog" />
	<atom:link rel='hub' href='http://chrisichris.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Programming for Android with Yeti</title>
		<link>http://chrisichris.wordpress.com/2011/07/31/programming-for-android-with-yeti/</link>
		<comments>http://chrisichris.wordpress.com/2011/07/31/programming-for-android-with-yeti/#comments</comments>
		<pubDate>Sun, 31 Jul 2011 17:44:21 +0000</pubDate>
		<dc:creator>chrisichris</dc:creator>
				<category><![CDATA[Yeti]]></category>

		<guid isPermaLink="false">https://chrisichris.wordpress.com/?p=93</guid>
		<description><![CDATA[The last tow years I was on search for a new programming-language for the JVM. First I started with Groovy, did a bit Clojure finally used scala a lot. This way I learned to appreciate modern statically typing with type-inference and the productivity of functional abstractions. The only thing which I did not like with [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chrisichris.wordpress.com&amp;blog=10115674&amp;post=93&amp;subd=chrisichris&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The last tow years I was on search for a new programming-language for the JVM. First I started with Groovy, did a bit Clojure finally used scala a lot. This way I learned to appreciate modern statically typing with type-inference and the productivity of functional abstractions. The only thing which I did not like with scala was its complexity. Therefore I was again searching another functional statically typed language and finally found yeti.</p>
<p><a href="https://github.com/mth/yeti/wiki" target="_blank">Yeti</a> is a functional language for the JVM, which is statically typed with full type-inference, powerful functional abstractions and structural-typing therefore allows much shorter code than Java. In this respect it is similar to scala but much simpler both in the type-system and  in the language features. (The language-tutorial is quite short and can be found here: <a href="http://linux.ee/~mzz/yeti/intro.html">http://linux.ee/~mzz/yeti/intro.html</a>)</p>
<p>By now I have done quite a bit of classical JVM programming with yeti and just wanted to test whether it also works for Android, because I thought it would be perfect for Android as well. Not just because it has the mentioned advantages over Java, but also because it has a smale language-api (about 177 KB) and because it is compiled and so quite fast.</p>
<p>In this post I want to show how to setup an Android project using ant to compile with yeti and in the end how to do the same thing with maven.</p>
<h3>Prerequisites</h3>
<p>1. If you don’t have it already, install the android-sdk and eclipse with the android plugin (<a href="http://developer.android.com/sdk/installing.html">http://developer.android.com/sdk/installing.html</a>)</p>
<p>2. Download the yeti.jar to its own directory this includes the compiler <a href="http://linux.ee/~mzz/yeti/yeti.jar" target="_blank">http://linux.ee/~mzz/yeti/yeti.jar</a></p>
<p>3. Download the yeti-lib.jar this are only the runtime-classes without the compiler <a href="http://linux.ee/~mzz/yeti/yeti-lib.jar" target="_blank">http://linux.ee/~mzz/yeti/yeti-lib.jar</a></p>
<p>(Alternatively you can also get the sources from github (<strong>git clone git://github.com/mth/yeti.git) </strong>and run ant for the yeti.jar and ant yeti-lib for the lib jar)</p>
<h3>Create a new Eclipse project with ant support</h3>
<p>1. In eclipse <strong>create a new Android Activity project</strong>. Exactly as described in the Android  “Hello World Tutorial: Create a New Android Project “: <a href="http://developer.android.com/resources/tutorials/hello-world.html#create">http://developer.android.com/resources/tutorials/hello-world.html#create</a></p>
<p>3. Run the newly created project from Eclipse (press the green run button and run as Android Activity)</p>
<p>2. Create an ant <strong>build.xml for your project</strong>: Open a command prompt and navigate to the base directory of you project and type android update project – path .</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:89f8b80f-792e-4bb9-b4d0-ac7c5d0318b4" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;"><pre class="brush: powershell; pad-line-numbers: true;">
&gt;android update project --path .
Updated local.properties
Added file c:\workspace\helloandroid\build.xml
</pre></p>
</div>
<p>If the android command is not found on the path then you need to update your path to include the Android tools. On windows at to the Path <code>%ANDROID_HOME%/tools</code>, where <span style="font-family:'Courier New';">%ANDROID_HOME%</span> points to the installation-directory of the android sdk.</p>
<h3>Add yeti-lib.jar Update build.xml and add –post-compile target with the yeti compiler</h3>
<p>1. Create in the base directory of your project a <strong>directory libs</strong> and copy the<strong> yeti-lib.jar</strong> there.</p>
<p>2. In eclipse right-click on the yeti-lib.jar and add it to the projects build-path.</p>
<p>3. modify the ant <code>build.xml </code>file to add the –post-compile target with the<strong> yeti compile task</strong>:</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:079978bf-82d9-4ad4-bb5c-4b9d01cef88a" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;"><pre class="brush: xml;">

   &lt;!-- insert following in build.xml before the already present &lt;setup/&gt; tag --&gt;

    &lt;!-- define the yeti compiler task --&gt;
    &lt;target name=&quot;yetic-def&quot;&gt;
        &lt;taskdef name=&quot;yetic&quot; classname=&quot;yeti.lang.compiler.YetiTask&quot;
                 classpath=&quot;C:/yeti/yeti.jar&quot;/&gt; &lt;!-- path to where you downlaoded the yeti.jar --&gt;
    &lt;/target&gt;

    &lt;!-- compile yeti sources after java sources have been compiled --&gt;
    &lt;target name=&quot;-post-compile&quot; depends=&quot;yetic-def&quot;&gt;
		&lt;yetic srcdir=&quot;${source.absolute.dir}&quot; destdir=&quot;${out.absolute.dir}&quot;
			   includes=&quot;**/*.yeti&quot;
			   preload=&quot;yeti/lang/std&quot;&gt;
			&lt;classpath&gt;
				&lt;path location=&quot;${out.absolute.dir}&quot;/&gt;
				&lt;pathelement location=&quot;${android.jar}&quot;/&gt;
				&lt;fileset dir=&quot;${native.libs.absolute.dir}&quot; includes=&quot;*.jar&quot;/&gt;
			&lt;/classpath&gt;
		&lt;/yetic&gt;

    &lt;/target&gt;

	&lt;!-- continues original build.xml --&gt;
	&lt;setup /&gt;

&lt;/project&gt;
</pre></p>
</div>
<h3>Update the build configuration of the eclipse project to include the yeti compiler ant-task</h3>
<p>In eclipse you can update the builders property to run the yeti compiler each time files are saved and modified. Because the yeti compiler is very fast this does not hinder your workflow.</p>
<p>1.) right-click on your project –&gt; select properties –&gt; choose Builders</p>
<p>2.) Create a new Ant Builder name it yetic</p>
<p>3.) in the first tab “Main” choose for “Buildfile:” the ant build.xml file we just have modified before, make sure the “Base Directory:” is ${workspace}/yourprojectname</p>
<p>4.) on the “Targets” tab choose in both “After a Clean” and “Auto Build” the –post-compile target from the build.xml</p>
<p>5.) press ok</p>
<h3>Finally rewrite your  HelloAndroid.java file in yeti and run it</h3>
<p>1.) delete the <code>HelloAndroid.java</code> file</p>
<p>2.) and rewrite the content of the <code>HelloAndroid.java</code> file in yeti: Create a file named “yetihello.yeti” in the same package as the HelloAndroid.java activity was and add the following content</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:e645c049-89d0-4539-b254-67835b0719a1" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;"><pre class="brush: java;">
module com.example.helloandroid.yetihello;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

class HelloAndroid extends Activity
	void onCreate(Bundle savedInstanceState)
		super#onCreate(savedInstanceState);
		tv = new TextView(this);
		tv#setText(&quot;Hello, Yeti on Android&quot;);
		this#setContentView(tv),

end;

()
</pre></p>
</div>
<p>3.) Press Run again for Hello Android</p>
<h2>Using Maven to build Android Application</h2>
<p>Finally I want to mention that you can also use maven-android-plugin together with the yeti-maven-plugin to build android applications using maven. All you have to do is setup a maven android project as described here  <a href="http://code.google.com/p/maven-android-plugin/">http://code.google.com/p/maven-android-plugin/</a> and add the yeti plugin as described here  <a href="https://github.com/chrisichris/yeti-maven-plugin">https://github.com/chrisichris/yeti-maven-plugin</a>. You should replace the dependency on yeti.jar with yeti-lib.jar and add the property yeti-full-jar=false in the yeti-plugin’s configuration section</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:dc4994bd-e724-47ce-a0e5-64c1eed2a8ac" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;"><pre class="brush: xml; pad-line-numbers: true;">
&lt;!-- yeti-lib dependency --&gt;
&lt;dependency&gt;
	&lt;groupId&gt;org.yeti&lt;/groupId&gt;
	&lt;artifactId&gt;yeti-lib&lt;/artifactId&gt;
	&lt;version&gt;0.1-SNAPSHOT&lt;/version&gt;
&lt;/dependency&gt;

&lt;!-- yeti plugin --&gt;
&lt;plugin&gt;
	&lt;groupId&gt;org.yeti&lt;/groupId&gt;
	&lt;artifactId&gt;yeti-maven-plugin&lt;/artifactId&gt;
	&lt;version&gt;0.1-SNAPSHOT&lt;/version&gt;
	&lt;executions&gt;
		&lt;execution&gt;
			&lt;goals&gt;
				&lt;goal&gt;compile&lt;/goal&gt;
				&lt;goal&gt;testCompile&lt;/goal&gt;
			&lt;/goals&gt;
		&lt;/execution&gt;
	&lt;/executions&gt;
	&lt;configuration&gt;
		&lt;yeti-full-jar&gt;false&lt;/yeti-full-jar&gt;
	&lt;/configuration&gt;
&lt;/plugin&gt;
</pre></p>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/chrisichris.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/chrisichris.wordpress.com/93/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/chrisichris.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/chrisichris.wordpress.com/93/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/chrisichris.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/chrisichris.wordpress.com/93/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/chrisichris.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/chrisichris.wordpress.com/93/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/chrisichris.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/chrisichris.wordpress.com/93/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/chrisichris.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/chrisichris.wordpress.com/93/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/chrisichris.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/chrisichris.wordpress.com/93/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chrisichris.wordpress.com&amp;blog=10115674&amp;post=93&amp;subd=chrisichris&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://chrisichris.wordpress.com/2011/07/31/programming-for-android-with-yeti/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/02ec6ed574f3db2aa3c11a2637a2ea2c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">chrisichris</media:title>
		</media:content>
	</item>
		<item>
		<title>Testing in yeti using yebspec</title>
		<link>http://chrisichris.wordpress.com/2011/04/01/testing-in-yeti-using-yebspec/</link>
		<comments>http://chrisichris.wordpress.com/2011/04/01/testing-in-yeti-using-yebspec/#comments</comments>
		<pubDate>Fri, 01 Apr 2011 10:19:06 +0000</pubDate>
		<dc:creator>chrisichris</dc:creator>
				<category><![CDATA[Yeti]]></category>

		<guid isPermaLink="false">https://chrisichris.wordpress.com/?p=90</guid>
		<description><![CDATA[After some first steps using the repl to learn yeti – the statically typed functional language for the jvm – you probably want to do a bit more to get a better feeling. In my experience one of the best ways to do so is to write unit tests. As it is easy to code [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chrisichris.wordpress.com&amp;blog=10115674&amp;post=90&amp;subd=chrisichris&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>After some first steps using the repl to learn <a href="http://chrisichris.wordpress.com/2011/02/24/yeti-probably-the-most-dynamic-of-the-statically-typed-languages-on-the-jvm/" target="_blank">yeti – the statically typed functional language for the jvm</a> – you probably want to do a bit more to get a better feeling. In my experience one of the best ways to do so is to write unit tests.</p>
<p>As it is easy to code regular Java classes in yeti you can write JUnit TestCases and do your unit-testing that way. However in this post I want to introduce you a more ‘yetish’ approach using yebspec a very simple test-module which is a somehow between classical unit-testing and the scala Behavior Driven-Design (BDD) test-framework <a href="http://code.google.com/p/specs/" target="_blank">specs</a>.</p>
<p>Yebspec is implementation-wise a very simple test-helper. It is just one module in the yeb project (<a href="https://github.com/chrisichris/yeb" target="_blank">https://github.com/chrisichris/yeb</a>) a still very alpha project for yeti. However yebspec is the part of the project which is most stable (and used).</p>
<p>In this blog-post I want to first show</p>
<ol>
<li>how to write a yebspec test than</li>
<li>how to run it from the console or through JUnit integration and finally</li>
<li>a bit how it is implemented</li>
</ol>
<h1>Writing Specifications to Specify and Test your code</h1>
<p>As said yebspec is implementation wise a very simple framework – much simpler than i. e. scala specs. All it does is to collect and report AssertionFailedError which might be thrown inside user defined specification functions by assert functions – exactly like in JUnit.</p>
<h2>Describe what Something Should do</h2>
<p>You basically create a dedicated spec module (similar to JUnit TestCase class) and define different specifications and assertions in it. This module can than be run and return a result-summary, which can be printed out or otherwise reported and analyst.</p>
<p>Let’s look at an example, where we test part of the <em>yeti.lang.std</em> module:</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:20e71ad1-7c50-486f-8eff-bb61c9d5cffb" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;">
<pre style="white-space:normal;"><pre class="brush: java; pad-line-numbers: true;">
module example.stdSpec;

load org.yeb.yebspec;

specificationsFor &quot;the yeti.lang.std module which is implicitly loaded in yeti&quot;\(

    specification &quot;for the list manipulation funtions&quot; \(
        baseList = [1 .. 20];

        describe &quot;map&quot; \(
            should &quot;apply function to each element&quot; \(
                r = map (+2) baseList;
                assertEquals [3..22] r;
            );
            should &quot;work on empty lists as well&quot; \(
                assertEquals [] (map id []);
            );
        );
    );

    specification &quot;for the string functions&quot; \(
        describe &quot;strLength&quot; \(
            should &quot;give right length&quot; \(assertEquals 3 (strLength &quot;123&quot;));
        );
    );
    //just describe threadLocal
    describe &quot;peekObject&quot; \(
        should &quot;read struct&quot; \(
		      ......
        )
    );
);
</pre>
</pre>
</div>
<p>To define specifications a new module is created. The result of the modules is the result of the one <em>specificationsFor</em> function.</p>
<p>The <em>specificationsFor</em> function takes a descriptive string and a function argument &#8211; \() &#8211; which gets evaluated once each time the result of <em>specificationsFor</em> is evaluated.</p>
<p>Inside the argument function the functions <em>specification, description and should</em> (collectively called subSpecs) are called which in turn take a description-string and a function argument, which can recursively  contain again subSpecs and different <em>assert</em> statements.    From the code-logic point of view there is no difference between <em>specification, describe und should</em>. They are all the same function only with a different name for better readability.</p>
<p>To compare this with JUnit: The module plus the <em>specificationsFor</em> function is similar to a TestCase class, the subSpecs (<em>specification, describe, should</em>) are the test-methods and which contain assert statements</p>
<h2>Asserting Behavior</h2>
<p>Like in JUnit in yebspec different assert functions can be used to assert that code does what it should do. (In BDD it is a central concept to use different names for assert, because there you do not test something but describe it, however yebspec uses assert like JUnit)</p>
<p>The assert statements must be executed in one of the subSpecs (<em>specification, describe, should</em>) argument functions. Like their JUnit counterparts assert functions throw <em>junit.framework.AssertionFailedErrors</em> if the assertion does not hold and this error is caught by the subSpecs and collected to report.</p>
<p>There are many different assert functions. Basic ones (fail, <em>assertTrue, assertFalse, assertEquals, assertNotEquals, assertSame, assertDefined</em>), some for lists (<em>assertEmpty, assertNotEmpty, assertContains, assertContainsAll, assertEqualsElements</em>) and some  for Exceptions (<em>assertException, assertFailWith, assertExceptionIs</em>). (To see how to use them it is currently best to look at the source code of src/main/java/org/yeb/yebspec.yeti module at <a href="https://github.com/chrisichris/yeb">https://github.com/chrisichris/yeb</a>)</p>
<p>If you want you can add a description to an assert using the assertLabel function:</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:aa46ac6c-bbad-4b35-8310-8df5bd3424df" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;">
<pre style="white-space:normal;"><pre class="brush: java;">
assertLabel &quot;some description&quot; \(assertTrue someCondition);
</pre>
</pre>
</div>
<p>Writing a new assert function is simple. Just check the condition and if it fails call <em>testFail</em> with the assert specific message. For example <em>assertEquals</em> is implemented this way:</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:81053511-4e78-4a38-9196-ca3fd10e7d9a" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;">
<pre style="white-space:normal;"><pre class="brush: java;">
assertEquals right test =
    if right != test then
        testFail (&quot;Should be [\(right)] but was [\(test)]&quot;)
    else () fi;
</pre>
</pre>
</div>
<p>The <em>testFail</em> function will throw the right exception and take care that the <em>assertLabel</em> is added.</p>
<h2>Fixtures</h2>
<p>The argument functions to <em>specificationsFor</em> and the <em>subSpecs</em> are normal yeti functions. Any code can be executed in them and the <em>subSpecs</em> are also normal functions which are invoked – no magic here.</p>
<p>Therefore all normal yeti idioms can be used to define fixtures etc. There is no need for <em>setUp() tearDown()</em> methods.</p>
<p>To define a fixture which is fresh for each <em>‘should’</em> you generally do the following:</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:df6836ca-7a11-4cc1-9580-bbc5e3cae05e" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;">
<pre style="white-space:normal;"><pre class="brush: java;">
describe &quot;array functions&quot; \(
    shouldFX desc fn =
        should desc \(fn (array [1,2,3]));

    shouldFX &quot;push&quot; do ar:
        push ar 4;
        assertEquals (array [1..4]) ar;
    done;

    shouldFX &quot;shift&quot; do ar:
        assertEquals 1 (shift ar);
        assertEquals (array [2,3]) ar;
    done;

);
</pre>
</pre>
</div>
<p>Here we define our own should function <em>shouldFX</em>. It takes the description-string and a function which gets a fresh array as test data. The <em>shouldFX</em> function than calls the standard <em>should</em> with the provided function with a fresh array. And then the <em>shouldFX</em> function is used like the standard should function to test the <em>push</em> and <em>shift</em> array functions from <em>yeti.lang.std</em>.</p>
<p>If you have to do some clean up you should wrap the provided function call in a <em>try finally yrt</em> (as standard in Java or yeti):</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:3a83ba1b-c618-47e9-a035-0146e5a0f9c5" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;">
<pre style="white-space:normal;"><pre class="brush: java;">
shouldFX desc fn = should desc \(
   stream = ....
   try fn stream finally stream#close() yrt
);

</pre>
</pre>
</div>
<p>To have a test-environment which you do not want to setup for each ‘should’ because it is too expensive (i.e. in integration testing a long starting service), you just wrap many should in a <em>try finally yrt</em>:</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:1e426813-257b-4644-b37c-6cebe75beee4" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;">
<pre style="white-space:normal;"><pre class="brush: java;">
describe &quot;expensive service&quot; \(
	service = ....
	try
		should &quot;provide this&quot; \(..);
		should &quot;provide that&quot; \(..);
		....
	finally
		closeService service;
   yrt;
);
</pre>
</pre>
</div>
<p>This is all standard yeti. There is no support form yebspec for fixtures, test-environments etc. because yeti provides it more concise itself than the framework could do.</p>
<h2>Composing a Suite out of different Yebspec Modules</h2>
<p>Like in JUnit different yebspec modules can be combined in a suite which looks to the outside itself like a yebspec. Suites can be combined themselves in suites and so on.</p>
<p>This comes handy if you want to run all the tests form one place or combine different places.</p>
<p>To do so define a module with the <em>specSuite</em> function which takes a list of yebspec modules:</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:774573e6-791b-4863-aae6-ae155f7d6fa7" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;">
<pre style="white-space:normal;"><pre class="brush: java;">
module example.allSpecSuite;

load org.yeb.yebspec;  

specSuite &quot;all specs for yeti lang&quot;
        [load example.stdSpec,
         load example.ioSpec,
         load example.fooSpec
         ]
</pre>
</pre>
</div>
<h1>Running from the Repl and JUnit Integration</h1>
<p>A specification module can be run from the regular yeti-console, more convenient through the  <a href="https://github.com/chrisichris/yeti-maven-plugin" target="_blank">yeti-maven-plugin</a> repl and through JUnit for build-tool and ide-integration.</p>
<h2>Running from the regular console</h2>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:9c6141b2-a6bc-47d8-83d8-ae2f9238dad4" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;">
<pre style="white-space:normal;"><pre class="brush: plain;">
yeti&gt;ys = load org.yeb.yebspec;
...
yeti&gt;res = (load example.stdSpec) none
....
yeti&gt;ys.printResult print res
....

</pre>
</pre>
</div>
<p>First the yebspec module is loaded, than the spec we have defined is evaluated and finally the result is printed.</p>
<p>There is a lot of uninteresting intermediate output, so you generally do this in one line:</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:45e61571-880b-4bd3-a002-dd17e8172e4b" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;">
<pre style="white-space:normal;"><pre class="brush: plain;">
yeti&gt;(load org.yeb.yebspec).printResult print ((load example.stdSpec) none)
-------------------------------
 Y e b   Y e t i   S p e c s
-------------------------------
success:   specifications for: the yeti.lang.std module which is implicitly loaded in yeti (s:6, f:0, e:0)
  success:   specification: for the list manipulation funtions (s:4, f:0, e:0)
    success:   describe: map (s:2, f:0, e:0)
      success:   should: apply function to each element
      success:   should: work on empty lists as well

    success:   describe: array (s:2, f:0, e:0)
      success:   should: push
      success:   should: shift

  success:   specification: for the string functions (s:1, f:0, e:0)
    success:   describe: strLength (s:1, f:0, e:0)
      success:   should: give right length

  success:   describe: peekObject (s:1, f:0, e:0)
    success:   should: read struct

Result: SUCCESS:
Specs run: 6, failed:0, exceptions:0
===== E N D   Y e b   Y e t i   S p e c s
</pre>
</pre>
</div>
<h2>Running from the yeti-maven-plugin Repl</h2>
<p>If you are using the yeti-maven-plugin (which is handy when developing with yeti <a href="http://chrisichris.wordpress.com/2011/02/25/the-yeti-maven-plugin-for-interactive-coding-and-running-yeti-code/" target="_blank">see a previous blog-entry</a>) there is additional support.</p>
<p>Especially you can run all or certain specs automatically each time you change a source-file. This is very helpful during development, because yeti immediately recompiles the tested source file and test it. This is very similar to what you get through modern Java-IDE’s instant-error reporting and even more because it does not only report compile errors but also test errors. And it does not hinder the workflow because yeti is fast &#8211; very fast. This way you get nearly both the interactive development features of dynamic language with the compile time safety of static-languages.</p>
<p>To enable the additional maven support add the yeb dependency and configure the maven-yeti repl to automatically load the <em>org.yeb.yebshell</em> module.  The <em>org.yeb.yebshell</em> module is the helper module which provides i.e. the repl support for yebspec.</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:dfeceafe-2f78-469c-a16e-02654642dda4" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;">
<pre style="white-space:normal;"><pre class="brush: xml;">
pom.xml:

&lt;dependency&gt;
    &lt;groupId&gt;org.yeb&lt;/groupId&gt;
    &lt;artifactId&gt;yeb&lt;/artifactId&gt;
    &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
&lt;/dependency&gt;

&lt;plugin&gt;
    &lt;groupId&gt;org.yeti&lt;/groupId&gt;
    &lt;artifactId&gt;yeti-maven-plugin&lt;/artifactId&gt;
    .....
    &lt;configuration&gt;
        &lt;commands&gt;
            ys = load org.yeb.yebshell;
        &lt;/commands&gt;
    &lt;/configuration&gt;
&lt;/plugin&gt;
</pre>
</pre>
</div>
<p>Start the maven repl from the command line and call –ys.checkSpec “module.name” to run spec once and  –ys.monitorSpec “module.name” to run a spec each time a source file is changed:</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:0e2acf31-f278-4b0e-a842-cdf95d912d9f" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;">
<pre style="white-space:normal;"><pre class="brush: plain;">
C:\yeti\example&gt;mvn -Dyetic=false clean yeti:repl
......
yeti&gt;-ys.checkSpec &quot;example.stdSpec&quot;
......
yeti&gt;-ys.monitorSpec &quot;example.stdSpec&quot;
.....
</pre>
</pre>
</div>
<p>Note the module-name is given as a string. This is because the tests are executed in an extra repl.</p>
<p>Use <em>–s.showMonitors ()</em> to show all current monitors and <em>–s.removeMonitor id</em> to remove a monitored spec.</p>
<h2>JUnit integration</h2>
<p>JUnit is of course excellent integrated in build-tools and IDEs etc – something yebspec is indeed not <img class="wlEmoticon wlEmoticon-winkingsmile" style="border-style:none;" src="http://chrisichris.files.wordpress.com/2011/04/wlemoticon-winkingsmile.png?w=594" alt="Zwinkerndes Smiley" />. However yebspec uses JUnit as a brigde to get to the same support us JUnit has ie nice graphical output from IDEs, automatic testing through maven ant etc.</p>
<p>If you want that – and you propably do &#8211; just create a Java JUnit <em>TestCase</em> class and load into the <em>suite()</em> method all the tests in your yebspec. To JUnit (and all the tools) this than looks than exactly like a regular JUnit 3 TestSuite:</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:b3b9d141-40e8-41dc-98ba-63cd9a321e06" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;">
<pre style="white-space:normal;"><pre class="brush: java;">
package example;

import junit.framework.Test;
import junit.framework.TestCase;
import org.yeb.YebUtils;

public class StdSpecTest extends TestCase {

    public StdSpecTest(String testName) {
        super(testName);
    }

    public static Test suite() {
        Test suite = YebUtils.createSuite(&quot;example.stdSpec&quot;);
        return suite;
    }

}
</pre>
</pre>
</div>
<p>That’s it now all your specs are run and reported as JUnit Tests by maven, Netbeans etc.</p>
<h1>Implementation</h1>
<p>As said the implementation is actually very simple. It is just one module where half of it is assert definitions and reporting. You can take a look at this module at the <a href="https://github.com/chrisichris/yeb" target="_blank">git-hub projects site</a> in src/main/java/org/yeb/yebspec.yeti</p>
<p>At the core there are these functions</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:a8e5e579-fdcd-4f70-839d-8bbe4a4f0635" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;">
<pre style="white-space:normal;"><pre class="brush: java;">
_newNode name is string -&gt; resultNode = (
    {name,
    var parent = none,
    var children = [],
    var result = Right (),
    var msg = &quot;&quot;,
    var rights = 0,
    var fails = 0,
    var exceptions = 0
    }
);

_parentNodeTL = (x is None () | Some resultNode = none; threadLocal x);

_specs maybeParentNodeIn name action is ( None () | Some resultNode ) -&gt; string -&gt; (resultNode -&gt; 'a) -&gt; resultNode= (
    //executes given action on each node and all its ancestors (parents)
    withParents node ac = (
        ac node;
        case node.parent of
            None _ : ();
            Some n : withParents n ac;
        esac;
    );

    maybeParentNode = case maybeParentNodeIn of
                         None _: _parentNodeTL.value;
                         Some _ : maybeParentNodeIn;
                      esac;

    //creaet node for esecuting this
    myNode = _newNode name;
    myNode.parent := maybeParentNode;
    case maybeParentNode of
        None _: ();
        Some pn : pn.children := pn.children ++ [myNode];
    esac;

    //set our node as the new thread local
    oldTLV = _parentNodeTL.value;
    _parentNodeTL.value := Some myNode;

    //execute
    try

        _ = action myNode;
        myNode.msg := &quot;&quot;;
        myNode.result := Right ();
        if empty? myNode.children then
            withParents myNode do n: n.rights := n.rights + 1 done;
        fi;
    catch AssertionFailedError ex:
        withParents myNode do n: n.fails := n.fails + 1 done;
        myNode.msg := &quot;fail: \(ex#getMessage())&quot;;
        myNode.result := Fail ex;
    catch java.lang.Exception ex:
        withParents myNode do n: n.exceptions := n.exceptions + 1 done;
        myNode.msg := &quot;exception: \(ex)&quot;;
        myNode.result := Exception ex;
    finally
        //reset the parent node
        _parentNodeTL.value := oldTLV;
    yrt;
    myNode;
);

specificationsFor name action maybeParentNode =
     _specs maybeParentNode &quot;specifications for: \(name)&quot; action;

_subSpec specName name action = (
    _ = _specs none (specName ^ name) action;
   ()
);

specification name action = _subSpec &quot;specification: &quot; name action,
describe name action = _subSpec &quot;describe: &quot; name action,
should name action = _subSpec &quot;should: &quot; name action,
</pre>
</pre>
</div>
<p>The central function here is <em>_specs</em>. This function takes an optional parent node (a node is a structure which describes in a tree the result of the evaluation of a subSpec) a description and a function which it executes and return a result node. If no parent node is given it checks he ThreadLocal _parentNodeTL for one. Than it creates a new node for its own result and adds it to the parent node (if there is one). And than it binds its new node as the parent-node to the ThreadLocal.</p>
<p>After that it just executes the given function and catches any AssertionFaildErrors or other Exceptions and adds the result of a failed or successful run to the node (and its parents).</p>
<p>The <em>specificationsFor</em> and <em>specification, describe</em> and <em>should</em> functions just delegate to the <em>_specs</em> function. The <em>specificationsFor</em> returns the node which can than be printed, analyzed, used for JUnit integration etc.</p>
<h1>Finally</h1>
<p>Well that’s about it. I think the most important point here is probably that you see how easy it is to write useful code in yeti.</p>
<p>Hope you like it and hope even more you comment on it. It just more rewarding if there are comments positive but of course also critiques.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/chrisichris.wordpress.com/90/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/chrisichris.wordpress.com/90/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/chrisichris.wordpress.com/90/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/chrisichris.wordpress.com/90/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/chrisichris.wordpress.com/90/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/chrisichris.wordpress.com/90/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/chrisichris.wordpress.com/90/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/chrisichris.wordpress.com/90/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/chrisichris.wordpress.com/90/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/chrisichris.wordpress.com/90/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/chrisichris.wordpress.com/90/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/chrisichris.wordpress.com/90/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/chrisichris.wordpress.com/90/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/chrisichris.wordpress.com/90/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chrisichris.wordpress.com&amp;blog=10115674&amp;post=90&amp;subd=chrisichris&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://chrisichris.wordpress.com/2011/04/01/testing-in-yeti-using-yebspec/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/02ec6ed574f3db2aa3c11a2637a2ea2c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">chrisichris</media:title>
		</media:content>

		<media:content url="http://chrisichris.files.wordpress.com/2011/04/wlemoticon-winkingsmile.png" medium="image">
			<media:title type="html">Zwinkerndes Smiley</media:title>
		</media:content>
	</item>
		<item>
		<title>Type Different: Less Code through Structural Typing or simple DI in Yeti on the JVM</title>
		<link>http://chrisichris.wordpress.com/2011/03/11/type-different-less-code-through-structural-typing-or-simple-di-in-yeti-on-the-jvm/</link>
		<comments>http://chrisichris.wordpress.com/2011/03/11/type-different-less-code-through-structural-typing-or-simple-di-in-yeti-on-the-jvm/#comments</comments>
		<pubDate>Fri, 11 Mar 2011 21:51:18 +0000</pubDate>
		<dc:creator>chrisichris</dc:creator>
				<category><![CDATA[Yeti]]></category>

		<guid isPermaLink="false">https://chrisichris.wordpress.com/?p=85</guid>
		<description><![CDATA[Yeti is a simple functional statically typed language for the JVM, of which I am  quite found of . In  this post I want to show how the flexibility of yeti&#8217;s type-system can be used to do type-safe dependency-injection (DI) with very little code. Before I come to the dependency injection part I want to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chrisichris.wordpress.com&amp;blog=10115674&amp;post=85&amp;subd=chrisichris&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://linux.ee/~mzz/yeti/intro.html" target="_blank">Yeti</a> is a simple functional statically typed language for the JVM, of which <a href="http://chrisichris.wordpress.com/2011/02/24/yeti-probably-the-most-dynamic-of-the-statically-typed-languages-on-the-jvm/" target="_blank">I am  quite found of</a> .</p>
<p>In  this post I want to show how the flexibility of yeti&#8217;s type-system can be used to do type-safe dependency-injection (DI) with very little code.</p>
<p>Before I come to the dependency injection part I want to introduce Yeti&#8217;s structural type-system, which is the reason you can do DI in the way I’ll show and which is at the same time quite different from the nominal type-systems used in most other languages like Java, C# or mainly Scala.</p>
<h2>When it walks like a duck and swims like a duck, can you see it as a duck?</h2>
<p>The answer to this question depends on the type-system.  (Note I only speak about static type-system in this post).</p>
<p>In Java, C#, Scala  type-names (or class-names) matter. In these languages only instances of the class with the same name (and its explicitly by name declared subclasses) have the same type and can therefore be used interchangeable. Instances of other classes have a different type no matter whether they possibly have exactly the same fields and methods or even have the same code apart of the their name – no duck-typing here. Types are differentiated in these languages by their defined names. Therefore the type-systems of these languages are called nominal type-systems.</p>
<p>Contrary in yeti every data-structure and each function which is composed of the same elements has exactly the same type – no matter how it is called. Names &#8211; if there are any at all &#8211; are just cosmetic. In OO terms you could say that if two objects have the same fields and methods than they always also have the same type and can be used interchangeable regardless of their class.</p>
<p>Actually in such a OO-language you would not have to explicitly define classes, because if you create an object with the methods <em>void</em> <em>duckSwim()</em> and <em>void</em> <em>duckWalk()</em> than it is automatically a member of the class of objects which can be used (seen) as a duck. And for the same reason in Yeti you do not have to define a named type or class and you even can not define a named type or class (you can define aliases but these are just shortcuts and you can for Java interoperability define Java-Classes but this is a different story).</p>
<p>In (pure) Yeti you just create a list, a structure, a function and if it has the same element-types like another list, structure etc. than it has the same type. And if a function accesses a field <em>duckSwim</em> on its argument and than calls the field&#8217;s value as a function than the compiler knows this parameter belongs to the set of structures (think records) which have a <em>duckSwim</em> field which in turn holds a function. And the compiler than requires the caller of the function to provide such a parameter, but how this is created, where it comes from, whatever other fields it has is unimportant.</p>
<p>Ok so lets first look at structures a bit closer with a small example so that we than have everything together for the main – the DI part.</p>
<h2>Structures as Record Data-Types</h2>
<p>Yeti has built in  primitive types (boolean, number, string and unit), function types and compositional types, which are used to compose all other types.</p>
<p>One of the built in compositional types are structures.</p>
<p>Structures are record data types that contain one or more named fields. Each of the fields can have a different value and has in turn its own type.</p>
<p>Structure values are created using structure literals, which contain in curly-braces one or more field definitions, separated by comma. Each field can contain any value &#8211; also functions or other structures. The values of a structure are accessed using the dot plus field-name.</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:a4f71eec-3a1e-45cf-9854-f4634be5f05a" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;">
<pre style="white-space:normal;"><pre class="brush: java; pad-line-numbers: true;">
&gt;pl = { distance = 2345,
            name=&quot;Jupiter&quot;,
            star= {name = &quot;Sun&quot;, volume = 345},
            rotate speed = .... //some function here
         } 

&gt;println pl.name
&quot;Jupiter&quot; is string
</pre>
</pre>
</div>
<p>Like you see we did not define a class &#8220;Planet&#8221; or so. We just created the runtime-instance very much like a dynamically typed data-structure, but still the compiler can calculate a type for each field as if we had defined a class &#8211; just without a name.  If you are interested in the type the compiler infers, you can enter the above structure at the yeti-repl (all in one line) and it will print out the full-type.</p>
<p>And if we write a function which i.e. prints out the star of a planet, we also do not have to first define a class planet to define the function argument type:</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:88eef533-6c9c-4cb6-8d7f-c33cce74d824" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;">
<pre style="white-space:normal;"><pre class="brush: java;">
printStarOfPlanet planet = (
     planetName = planet.name;
     starName = planet.star.name;
     println &quot;\(planetName) belongs to \(starName)&quot;
)
</pre>
</pre>
</div>
<p>Here we also work very much like with a dynamic data-structure, but also this one is statically type-checked, because the compiler demands that the parameter <code><em>planet</em></code> has a <span style="font-family:'Courier New';">name</span> field and a <span style="font-family:'Courier New';"><em>star</em></span> field which in turn has another <em>name</em> field. The compiler however does not demand that the structure has a <em>distance</em> or <em>rotate</em> field. It can have other fields but it does not have to. In fact any structure which has the <em>name</em> and <em>star.name</em> field can be printed with <em>printStarOfPlanet</em>.</p>
<p>It is obvious that this provides a lot of flexibility, because you do not have to define interfaces, sub-type-relationships etc. beforehand, but can just mix and match the demanded fields as needed when you create the structure.</p>
<p>So lets use that to tackle the common task of dependency-injection in a statically type checked manner, in a (possible) hierarchical fashion and scoped. And all that straight forward just in plain yeti  without any magic DI-framework.</p>
<h2>Elegant type-safe Dependency-Injection</h2>
<p>The example is an extended form of the example used in the excellent blog-posts: ”<a href="http://debasishg.blogspot.com/2011/03/pushing-envelope-on-oo-and-functional.html" target="_blank">Pushing the envelope on OO and functional with Scala</a>” by Debasish Ghosh and &#8220;<a href="http://jonasboner.com/2008/10/06/real-world-scala-dependency-injection-di.html">Real-World Scala: Dependency Injection (DI)</a>&#8221; by Jonas Bonér. This blog-posts are as their name say about DI in Scala, which is of course another very powerful modern JVM language &#8211; with a much more powerful type-system and feature-set than yeti but also more complicated (especially to read and understand).</p>
<p>To make things a bit more realistic and to show some features we use a few components:</p>
<ol>
<li>A <em>requestHandler</em> function which handles a <em>HttpServletRequest</em>, loads the current user from the session and than delegates to the <em>renderPage</em> function</li>
<li>the <em>renderPage</em> function that renders the html and uses the <em>productService</em> to render the product-of-the-month recommended to the current user</li>
<li>the <em>userService</em> – which for the example just delegates to the DAO</li>
<li>the <em>productService</em> &#8211; which also just delegates to the DAO</li>
<li>and a <em>DAO</em> for both the userService and the productService; one <em>dummyDAO</em> and one <em>productionDAO</em></li>
<li>a <em>HttpServlet</em> which plumbs all together</li>
</ol>
<p>I will not implement the full code just what is needed to show the DI concept, but still this sounds like a lot of code and interfaces just for a blog-post. Well just scroll down to the end of the post to see all the code together.</p>
<p>First we implement the <em>userService</em> and a <em>dummyDAO</em>. We put all the code in the<em> yetidi.yeti</em> file, which is the module which contains all the code.</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:b5a17cd7-aa7f-49df-922e-563aeffb022d" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;">
<pre style="white-space:normal;"><pre class="brush: java;">
module yetidi;

import javax.servlet.http.HttpServletRequest;

di = {

    userService ctxt = {
        loadUser userId = ctxt.userDAO.readUser userId,
        storeUser user = ctxt.userDAO.createUser user.name user.userId,
        removeUser userId = ctxt.userDAO.deleteUser userId,
    },

    dummyDAO = {
        readUser name = {userId = name, name = name},
        createUser name uid = {userId = uid, name = name},
        deleteUser name = true,
        readProduct name = {itemNumber = name, name = name}
    },
};

di
</pre>
</pre>
</div>
<p>Like in a typical yeti module we create a main structure which contains all the fields/functions of the module and return that at the end. (We store the main-structure in a variable, because we will need it for testing- which we do for brevity just in the module and we also want to put the <em>HttpServlet</em> class in the same module and this has to access this structure).</p>
<p>First you will note that we do not define any interfaces or classes, we code right away the implementation of the production and dummy components.</p>
<p>The first function <em>userService</em> takes a ctxt (context) argument and creates a structure which loads, stores, removes Users – the <em>userService</em>. The <em>userService</em> in turn looks up its DAO in the passed in context.</p>
<p>You might wonder why a context is passed in and not the <em>userDAO</em> directly. The reason is that the <em>userService</em> could itself create another component with dependencies. Now instead of declaring this additional dependencies the <em>userService</em> just passes <em>ctxt</em> further and the compiler will itself take care (infer) that the context has all the dependencies. We will see that later in the requestHandler function.</p>
<p>As the userService delegates all calls to the userDAO we also create a dummyDAO with the needed functions (see again no interface here).</p>
<p>Before going further the <em>dummyDAO</em> is used to test the userService. At the following two lines to the very end of the module (until the &#8212;&#8212;) and load the module in the repl.</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:ec3fe365-e3a4-4391-ae91-d761237feb8a" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;">
<pre style="white-space:normal;"><pre class="brush: java;">
uS = di.userService {userDAO = di.dummyDAO};
uS.loadUser &quot;foo&quot;;
--------
//result is:
{name=&quot;foo&quot;, userId=&quot;foo&quot;} is {name is string, userId is string}
</pre>
</pre>
</div>
<p>First the <em>userService</em> is created, using our <em>dummyDAO</em> and than we load the user “foo”. As expected this returns a structure with <em>name=”foo”</em> and <em>userId=”foo”</em>. Note that the compiler has inferred the full-type <em>{name is string, userId is string}</em>.</p>
<p>Everything is fine so we go on with the <em>productService</em>:</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:a8d0a31f-a52d-4350-a367-da336966339c" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;">
<pre style="white-space:normal;"><pre class="brush: java;">
productService ctxt = {
    loadProduct name = ctxt.productDAO.readProduct name,
    productOfTheDay userId = (
        u = ctxt.userService.loadUser userId;
        //choose some product for the user and load it
        loadProduct &quot;itemFor-\(u.name)&quot;
    ),
},

dummyDAO = {
    readUser name = {userId = name, name = name},
    createUser name uid = {userId = uid, name = name},
    deleteUser name = true,
    readProduct name = {itemNumber = name, name = name}
},
</pre>
</pre>
</div>
<p>The <em>productService</em> is similar implemented as the <em>userService</em>: It takes a context from which it looks up its <em>productDAO</em> and just delegates to it.</p>
<p>Additional it provides a <em>productOfTheDay</em> which is used by the view to show an advertisement for a given customer. The <em>productOfTheDay</em> function in turn uses the <em>userService</em>. So the <em>productService</em> has two dependencies the <em>productDAO</em> and the <em>userService</em>.</p>
<p>Finally we just extend our <em>dummyDAO</em> with the <em>readProduct</em> function – in production we could separate the DAOs.</p>
<p>Lets test it again:</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:97276356-9782-4cb2-8626-f9eed88addc7" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;">
<pre style="white-space:normal;"><pre class="brush: java;">
uS = di.userService {userDAO = di.dummyDAO};
pS = di.productService {productDAO = di.dummyDAO, userService = uS};

pS.productOfTheDay &quot;Joe&quot;;
-------------------
{itemNumber=&quot;itemFor-Joe&quot;, name=&quot;itemFor-Joe&quot;} is {itemNumber is string, name is string}
</pre>
</pre>
</div>
<p>Again we first create the userService , than we create the <em>productService</em> injecting the <em>productDAO</em> and the <em>userService</em> we have just created. And finally we get as expected an item with the right type. Note that we have not once defined a type or interface, no parameter type, no item class, no return-type nothing.</p>
<p>But could we now test the <em>productDAO</em> without using the real <em>userService</em>? Sure:</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:f2badaf3-dd07-4280-a313-79a24bd6ab18" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;">
<pre style="white-space:normal;"><pre class="brush: java;">
pS = di.productService {productDAO = di.dummyDAO,
                        userService = {
                            loadUser ui = {userId = &quot;foo\(ui)&quot;,
                                               name = &quot;foo\(ui)&quot;}}};
pS.productOfTheDay &quot;Joe&quot;;
------
{itemNumber=&quot;itemFor-fooJoe&quot;, name=&quot;itemFor-fooJoe&quot;} is {itemNumber is string, name is string}
</pre>
</pre>
</div>
<p>This time we inject through the context a mock <em>userService</em> which we create on the fly. And additional we only implement the function which is actually needed by the <em>productService</em> (<em>loadUser</em>) and not all the functions which are by chance on a fictive interface of <em>userService</em> because the “interface” of <em>userService</em> from the point of the <em>productService</em> is just the function <em>loadUser</em>. That’s what the compiler says, because all this is still completely statically type-checked. I.e. If we would misspell l<strong>ao</strong>dUser the compiler would immediately complain.</p>
<p>The final two pieces of our puzzle are the requestHandler and the renderPage. Both are just functions not structs, because the only do one thing (which is nice because yeti is after all a functional language):</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:b7c26975-5101-40a3-ba25-f16cec39ca12" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;">
<pre style="white-space:normal;"><pre class="brush: java;">
    requestHandler ctxt req is 'a -&gt; ~HttpServletRequest -&gt; 'b = (
        userId = if defined? req#getSession()#getAttribute(&quot;user-id&quot;) then
                    string req#getSession()#getAttribute(&quot;user-id&quot;)
                 else
                    &quot;anonymous&quot;
                 fi;
        user = ctxt.userService.loadUser userId;

        scopedCtxt = ctxt with {user = user};
        content = renderPage scopedCtxt;
        {status=200, headers = [&quot;Content-Type&quot;:&quot;text/html;charset=UTF-8&quot;], content}
    ),
</pre>
</pre>
</div>
<p>As always the <em>requestHandler</em> takes a <em>ctxt</em> which contains the dependencies additional it takes a <em>HttpServletRequest</em> argument<em>.</em></p>
<p>Because the <em>req</em> argument is a Java-Class we have to use the first time a type-annotations (is …). For a description of what type-annotations are, when and how to use them  please read the yeti introduction. But in short they are not only needed when Java-Classes are involved, they are also very handy when you are lost in the types, because than you can annotate functions, variables etc. with types and the compiler checks whether the types you expect match the types the compiler infers.</p>
<p>After the <em>requestHandler</em> has loaded the user of the current session it creates a <strong>scopedCtxt</strong>.</p>
<p>The <em>scopedCtxt</em> is a copy of the original <em>ctxt</em> enriched with the user. The with keyword means: take the structure on the left, copy it and add to the copy all the fields from the right structure (or replace them if they have the same name).</p>
<p>And this is of course great to create a scoped context for the current request only, which contains exactly the same dependencies as the global context but additional the current user  – without bothering the receiver of the scopedCtxt (<em>renderPage</em>) with what is in which scopes.</p>
<p>Finally we call the renderPage function. This function while it is also a dependency is not injected, because also in practice you of do not inject each dependency especially if you do not have to be super-flexible and for readability prefer a more direct style, or because it is a deep hierarchy where injection would be just to much work and in our example we do not inject the renderPage because we want to demonstrate the flexibility of the context.</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:91c1d403-41de-450b-9dc6-54c26ea77be6" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;">
<pre style="white-space:normal;"><pre class="brush: xml;">
    renderPage ctxt = (
       user = ctxt.user;
       productOfDay = ctxt.productService.productOfTheDay user.userId;
       &quot;&quot;&quot;&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;
           &lt;div id=&quot;product&quot;&gt;Hi \(user.name), this is nice: \(productOfDay.name)&lt;/div&gt;
          &lt;/body&gt;&lt;html&gt;
       &quot;&quot;&quot;;
    ),
</pre>
</pre>
</div>
<p>As you can see we take the user from the scoped ctxt which the <em>requestHandler</em> provides, but the interesting part here is the dependency on the <em>productService</em>.</p>
<p>But the interesting thing here is that the <em>renderPage</em> takes (like always) the <em>productService</em> from the context as a dependency. However without knowledge of the <em>requestHandler</em> or declaration of this dependency in the <em>requestHandler</em> itself, although the <em>requestHandler</em> has alone chosen to use the renderPage function and injects everything needed into this function. But while the dependency of the <em>renderPage</em> is completely transparent to the requestHandler the one who uses the requestHandler must provide this dependency – enforced by the compiler.  And if you would decide that <em>renderPage</em> needs yet another dependency you just take it from the context without modifying the <em>requestHandler</em> at all and the compiler will again enforce that the original ctxt contains this new <em>dependency</em>.</p>
<p>The dependency is passed totally transparently  through the <em>requestHandler</em> and also the other way round: the static type-requirement for the dependency is passed totally transparent to the caller of the <em>requestHandler</em> through the <em>requestHandler</em></p>
<p>I leave the unit-testing to you and just want to finally plug everything together in a HttpServlet (of course coded in yeti):</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:a1ae81a3-056b-4234-ae47-16c57fff064a" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;">
<pre style="white-space:normal;"><pre class="brush: java;">
productionDAO = {
        readUser name = {userId = name, name = name},
        createUser name uid = {userId = uid, name = name},
        deleteUser name = true,
        readProduct name = {itemNumber = name, name = name}
},

class YetidiServlet extends HttpServlet

    handler = (

        uS = di.userService {userDAO = di.productionDAO};
        pS = di.productService {productDAO = di.productionDAO, userService = uS};
        di.requestHandler {userService = uS, productService = pS, user = {userId = &quot;an&quot;, name = &quot;an&quot;}}
    ),

    void doService (HttpServletRequest req, HttpServletResponse res)
         c= handler req;
         res#setStatus(c.status);
         forHash c.headers do key value: res#setHeader(key,value); done;
         wr = res#getWriter();
         wr#print(c.content);
         wr#flush(),

end;
</pre>
</pre>
</div>
<p>At the end of the blog you can find the full code. I just want to add that – at least I – would have a very hard time to do statically type-checked DI in a possibly hierarchical style with scoped contexts just with the means of the language in as little code with any of the other statically typed JVM languages.</p>
<h2>But are there disadvantages in structural typing and if so are they worth it?</h2>
<p>Unfortunately yes there are disadvantages:</p>
<ol>
<li>Structural typing is (arguably) more flexible than nominal typing, but this comes at the price that certain mistakes are not caught by the compiler: I.e. if you have a customer structure which you pass mistakenly to a function which expects a supplier which has the same fields (i.e. name, balance) than yeti wont catch that. If you want you can easily prevent this with using Variant Tags (some thing for a later post), but you must do that explicitly while nominative type-systems enforce this always</li>
<li>The yeti-type-system adds more limitations to otherwise legal code than more complex type-systems which require type-annotations. I.e. you can not create structures with polymorphic functions through function application. So in our example the productService and userService could not have polymorphic functions. However for “business-service” structures (instead of libraries) you do not that often need polymorphic functions. And if you need them there are ways to work around the limitation (standalone polymorphic functions which take the contexts via a parameter or you could always trick the compiler via casting).</li>
<li>Another disadvantage (at least in yeti) is that because the types have no names and are only described by there structure, you often get very long hard to read type-descriptions in compile error-messages.</li>
<li>Subtyping  is also better supported in nominal type-systems, but also here Variant (or algebraic data-types) are often the better choice and are supported by yeti. In this context it is also notable, that yeti also supports recursive Structures and Variants without explicit declarations</li>
</ol>
<p>But IMO all these disadvantages do not justify the more code and lost flexibility you get with the nominal type-systems.</p>
<p>And this seems to be confirmed by the fact that structural typing gets more popular in recent time. Googles Go language supports it, also scala (while it is not the “default”) and the more and more popular functional languages Haskell and ML (the origin of yeti) as well.</p>
<p>The best thing is to try it yourself. Just take a final look at the DI example code and give yeti a try. It is easy to learn, because it is simple and still has very powerful possibilities.</p>
<p>In my experience it is a real pleasure to work with yeti, unmatched by any language on the JVM.</p>
<p>&nbsp;</p>
<h2>Appendix: The complete sample code</h2>
<p>.</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:6d45603e-06ad-47cf-97ac-8d5a8a4aac97" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;">
<pre style="white-space:normal;"><pre class="brush: java;">
module yetidi;

import javax.servlet.http:HttpServletRequest, HttpServletResponse, HttpServlet;

di = {

    userService ctxt = {
        loadUser userId = ctxt.userDAO.readUser userId,
        storeUser user = ctxt.userDAO.createUser user.name user.userId,
        removeUser userId = ctxt.userDAO.deleteUser userId,
    },

    productService ctxt = {
        loadProduct name = ctxt.productDAO.readProduct name,
        productOfTheDay userId = (
            u = ctxt.userService.loadUser userId;
            //choose some product for the user and load it
            loadProduct &quot;itemFor-\(u.name)&quot;
        ),
    },

    dummyDAO = {
        readUser name = {userId = name, name = name},
        createUser name uid = {userId = uid, name = name},
        deleteUser name = true,
        readProduct name = {itemNumber = name, name = name}
    },

    productionDAO = {
        readUser name = {userId = name, name = name},
        createUser name uid = {userId = uid, name = name},
        deleteUser name = true,
        readProduct name = {itemNumber = name, name = name}
    },

    requestHandler ctxt req is 'a -&gt; ~HttpServletRequest -&gt; 'b = (
        userId = if defined? req#getSession()#getAttribute(&quot;user-id&quot;) then
                    string req#getSession()#getAttribute(&quot;user-id&quot;)
                 else
                    &quot;anonymous&quot;
                 fi;
        user = ctxt.userService.loadUser userId;

        scopedCtxt = ctxt with {user = user};
        content = renderPage scopedCtxt;
        {status=200, headers = [&quot;Content-Type&quot;:&quot;text/html;charset=UTF-8&quot;], content}
    ),

    renderPage ctxt = (
       user = ctxt.user;
       productOfDay = ctxt.productService.productOfTheDay user.userId;
       &quot;&quot;&quot;&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;
           &lt;div id=&quot;product&quot;&gt;Hi \(user.name), this is nice: \(productOfDay.name)&lt;/div&gt;
          &lt;/body&gt;&lt;html&gt;
       &quot;&quot;&quot;;
    ),

};

class YetidiServlet extends HttpServlet

    handler = (

        uS = di.userService {userDAO = di.productionDAO};
        pS = di.productService {productDAO = di.productionDAO, userService = uS};
        di.requestHandler {userService = uS, productService = pS, user = {userId = &quot;an&quot;, name = &quot;an&quot;}}
    ),

    void doService (HttpServletRequest req, HttpServletResponse res)
         c= handler req;
         res#setStatus(c.status);
         forHash c.headers do key value: res#setHeader(key,value); done;
         wr = res#getWriter();
         wr#print(c.content);
         wr#flush(),
end;

di;

</pre>
</pre>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/chrisichris.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/chrisichris.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/chrisichris.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/chrisichris.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/chrisichris.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/chrisichris.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/chrisichris.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/chrisichris.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/chrisichris.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/chrisichris.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/chrisichris.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/chrisichris.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/chrisichris.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/chrisichris.wordpress.com/85/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chrisichris.wordpress.com&amp;blog=10115674&amp;post=85&amp;subd=chrisichris&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://chrisichris.wordpress.com/2011/03/11/type-different-less-code-through-structural-typing-or-simple-di-in-yeti-on-the-jvm/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/02ec6ed574f3db2aa3c11a2637a2ea2c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">chrisichris</media:title>
		</media:content>
	</item>
		<item>
		<title>Yeti Editor Support in Vim, Netbeans and Notepad++</title>
		<link>http://chrisichris.wordpress.com/2011/02/26/yeti-editor-support-in-vim-netbeans-and-notepad/</link>
		<comments>http://chrisichris.wordpress.com/2011/02/26/yeti-editor-support-in-vim-netbeans-and-notepad/#comments</comments>
		<pubDate>Sat, 26 Feb 2011 19:00:39 +0000</pubDate>
		<dc:creator>chrisichris</dc:creator>
				<category><![CDATA[Yeti]]></category>

		<guid isPermaLink="false">http://chrisichris.wordpress.com/?p=50</guid>
		<description><![CDATA[In my last post I wrote about how to use maven with Yeti. Today I want to write about the &#8211; still basic &#8211; editor support for yeti. Yeti has editor-support for vim, notedpad++ (windows) and Netbeans. It is very limited - basically syntax higlighting and brace matching etc. Still try it out. All of them take [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chrisichris.wordpress.com&amp;blog=10115674&amp;post=50&amp;subd=chrisichris&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In my last post I wrote about how to use maven with Yeti. Today I want to write about the &#8211; still basic &#8211; editor support for yeti.</p>
<p>Yeti has editor-support for vim, notedpad++ (windows) and Netbeans. It is very limited - basically syntax higlighting and brace matching etc.</p>
<p>Still try it out.</p>
<p>All of them take you a minute to install. None of them is buggy (because they are simple).  And eyes just like colors.</p>
<p>I&#8217;ll shortly describe each of them below. If you are in a hurry skip the next paragraph.</p>
<h1>A few Words on IDE Support in General</h1>
<p>I think if you chose/try a a new language you should really see how it does with a simple plain editor, because in programming the language is the cook and the IDE is the waiter when you are the restaurant owner.</p>
<p>The best IDE with code-completition, -analysis, refactoring, generation what so ever can only go so far as  the language lets it and has to go so far because the language needs it.</p>
<p>And for that reason none of the most popular languages today started out with excellent IDE support. In contrary most of the time the falling languages have had the best IDE support because IDEs have to make good what the languages do not support but newer competing languages do.</p>
<p>This does of course not mean that IDE support is useless &#8211; of course it would also be very nice if  Yeti had Java-quality IDE support. However it is no way as important for Yeti as ie for Java for different reasons:</p>
<ul>
<li>Yeti code is compact so you do not type that much especially foreign defined identifiers like classnames for typing where ie code-completetion helps a lot</li>
<li>For the same reason you do not have to read that much and an outline-few is not such a must.</li>
<li>There are also not huge apis with hundreds of classes each having in turn tenth of  (partly inherited) methods so again code-completetion documentation-popup etc is not that important.</li>
<li>The yeti-compiler is very fast and with the continues compilation possibility shown in the last post you do have very short edit/run/test cycles, which makes error-higlightning less important</li>
</ul>
<p>Enough said here are the supported Editors/IDEs.</p>
<h1>Vim</h1>
<p>Vim support comes form the creator of Yeti Madis himself. You find the syntax and filetype plugin at the home page of yeti <a href="http://mth.github.com/yeti/">http://mth.github.com/yeti/</a>.</p>
<p>To install the plugin follow this quotation from the Yeti homepage:</p>
<blockquote><p>Vim <a href="http://github.com/mth/yeti/tree/master%2Futil%2Fsyntax%2Fyeti.vim?raw=true">syntax hilighting</a> and <a href="http://github.com/mth/yeti/tree/master%2Futil%2Fftplugin%2Fyeti.vim?raw=true">filetype plugin</a>.<br />
Save these files as <tt>~/.vim/syntax/yeti.vim</tt> and <tt>~/.vim/ftplugin/yeti.vim</tt> accordingly, and add<br />
<tt><strong>au BufNewFile,BufRead *.yeti setlocal filetype=yeti ts=8 sw=4 sts=4 expandtab</strong></tt><br />
into your <tt>~/.vimrc</tt>.</p></blockquote>
<h1>Notepad++</h1>
<p>Notepad++ is one of the most popular Editors for Windows.</p>
<p>You can download  a &#8220;user defined language&#8221; file for Yeti :  <a href="http://chrisichris.github.com/chrisis-maven-repo/notepadplusplus_yeti.xml">http://chrisichris.github.com/chrisis-maven-repo/notepadplusplus_yeti.xml</a> (right-click the link to save the file).</p>
<p>To install:  download the above file, open Notepad++  go to  &#8221;View &gt; User-Defined Dialogue&#8230;&#8221; press the button &#8220;Import&#8230;&#8221;.  and open the file. Done.</p>
<p>Note: The &#8220;Import..&#8221; function is only supported in current versions. If your Version does not support it either upgrade (which you should anyway) or Google on how to install user defined language files in older Versions its not that much more work.</p>
<h1>Netbeans</h1>
<p>You can also find a basic Netbeans plugin for Netbeans 6.8 and 6.9 here: <a href="https://github.com/chrisichris/nbyeti">https://github.com/chrisichris/nbyeti</a></p>
<p>You can download the module (org.nbyeti-6-8-01.nbm) file from the downloads section of the above github page.</p>
<p>You install it like any other Netbeans-module you have downloaded &#8211; nothing special there. See the the link above for detailed installation instructions.</p>
<p>If you are choosing Netbeans for your Yeti-devolopment I recommend you to also use the maven-netbeans-plugin and than create a maven yeti project as described in my last blog-post. This way you have a full integrated yeti/java netbeans project within a few minutes.</p>
<p>I hope that helps a bit.</p>
<p>If you have any questions use the yeti mailing-list: <a href="http://groups.google.com/group/yeti-lang">http://groups.google.com/group/yeti-lang</a> . See you there.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/chrisichris.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/chrisichris.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/chrisichris.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/chrisichris.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/chrisichris.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/chrisichris.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/chrisichris.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/chrisichris.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/chrisichris.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/chrisichris.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/chrisichris.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/chrisichris.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/chrisichris.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/chrisichris.wordpress.com/50/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chrisichris.wordpress.com&amp;blog=10115674&amp;post=50&amp;subd=chrisichris&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://chrisichris.wordpress.com/2011/02/26/yeti-editor-support-in-vim-netbeans-and-notepad/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/02ec6ed574f3db2aa3c11a2637a2ea2c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">chrisichris</media:title>
		</media:content>
	</item>
		<item>
		<title>The Yeti-Maven-Plugin for interactive coding and running Yeti-Code</title>
		<link>http://chrisichris.wordpress.com/2011/02/25/the-yeti-maven-plugin-for-interactive-coding-and-running-yeti-code/</link>
		<comments>http://chrisichris.wordpress.com/2011/02/25/the-yeti-maven-plugin-for-interactive-coding-and-running-yeti-code/#comments</comments>
		<pubDate>Fri, 25 Feb 2011 13:56:15 +0000</pubDate>
		<dc:creator>chrisichris</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Yeti]]></category>

		<guid isPermaLink="false">http://chrisichris.wordpress.com/?p=28</guid>
		<description><![CDATA[As pointed out in my last post Yeti http://mth.github.com/yeti/ is functional statically typed programming language for JVM with full typeinference. The language is simple but still gives a lot of power https://github.com/mth/yeti/wiki. In its feeling it comes very close to the agility of dynamic-languages, but of course remaining statically typechecked. One way to run Yeti-Code is to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chrisichris.wordpress.com&amp;blog=10115674&amp;post=28&amp;subd=chrisichris&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>As pointed out in my last post Yeti <a href="http://mth.github.com/yeti/">http://mth.github.com/yeti/</a> is functional statically typed programming language for JVM with full typeinference. The language is simple but still gives a lot of power <a href="https://github.com/mth/yeti/wiki">https://github.com/mth/yeti/wiki</a>. In its feeling it comes very close to the agility of dynamic-languages, but of course remaining statically typechecked.</p>
<p>One way to run Yeti-Code is to use the yeti.jar directly. This is very well described in the yeti tutorial http://linux.ee/~mzz/yeti/intro.html.</p>
<p>In this post I want to show you how to use the yeti-maven-plugin <a href="https://github.com/chrisichris/yeti-maven-plugin">https://github.com/chrisichris/yeti-maven-plugin</a></p>
<h2>Advantages of using the yeti-maven-plugin</h2>
<p>When you hear maven-plugin do not shy away <em>it is not more work</em> than using the yeti.jar directly, contrary it gives you a few advantages:</p>
<ul>
<li>It  is easier to manage the classpath especially using jars both for running with the REPL and compiling</li>
<li>It is a standard way to compile and test, distribute etc your yeti-code and integrates well if you want to also code parts in Java (or other JVM- languages)</li>
<li>yetidoc can be produced</li>
<li>a mavenproject is understood by all ides. A pure YETI-project as above not</li>
</ul>
<h2>Starting a Yeti Maven project and basic Usage</h2>
<p>To start a maven-yeti-project you have to</p>
<ol>
<li>Install maven 2 or greater (If you are a Java-Programmer you very propably have that already) <a href="http://maven.apache.org/run-maven/index.html">http://maven.apache.org/run-maven/index.html</a></li>
<li>Download the yeti-project-template.zip file from the downloads section of the yeti-maven-plugin <a href="https://github.com/chrisichris/yeti-maven-plugin">https://github.com/chrisichris/yeti-maven-plugin</a></li>
<li>Unpack the file in a directory of your choice and name it as you project.</li>
<li>Change in the pom.xml file the contents of the groupId, artifactId and name tags to your projects name<br />
<pre class="brush: xml; first-line: 4;">
    &lt;groupId&gt;your.groupId&lt;/groupId&gt;
    &lt;artifactId&gt;your-artifactId&lt;/artifactId&gt;
    &lt;packaging&gt;jar&lt;/packaging&gt;
    &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
    &lt;name&gt;your projects name&lt;/name&gt;
             </pre></li>
</ol>
<p>Now your maven project is setup and read to use.</p>
<p>You can put your yeti module files in the src/main/yeti directory or side-by-beside with your java files in src/main/java &#8211; for test files the corresponding src/test directories are used.</p>
<p>You can use all the mvn commands ie <code>mvn compile</code> <code>mvn test</code> <code>mvn install</code> In all these commands the yeti sources get compiled, tested, jared up etc exactly like the Java sources.</p>
<p>And you can start the interactive REPL:</p>
<p><pre class="brush: plain;">
mvn -Dyeti-compile=false clean compile yeti:repl
</pre></p>
<p>This will first clean than compile the Java sources and start the REPL with the full project classpath (including test-classes dependencies etc). The yeti sources are not compiled in this case (<code>-Dyeti-compile=false</code>), because the REPL will pick up the sources itself directly and compile them.</p>
<h2>Interactive Development with the REPL</h2>
<p>Yeti is new so Yeti does not yet have the IDE support Java has.</p>
<p>Among other things that means that when you code Yeti the IDE does not show you instantly compile errors you might have made. Instead you always have to edit your file, save it go to the command prompt, call compile (or reload the module in the repl), see a compile-error, fix it and start all over again. That&#8217;s boring lost time.</p>
<p>The yeti-maven-plugins REPL helps here.</p>
<p>In the repl you can define arbitrary yeti-code which gets executed each time you change sources.</p>
<p>You can use this facility to execute <code>load module.name</code> commands each time you save your changed code. This way the REPL will (re)compile the module  immidiately in the console, and you do not have to repeat the above circle.</p>
<p>Try it out: create a yeti module: src/yeti/example/immediateCompile.yeti (if you have started the project from the yeti-project-template.zip file &#8211; as described above &#8211; this file is already present)</p>
<p><pre class="brush: plain;">
module example.immediateCompile;
&quot;just an example showing the immediate compile function of the repl&quot;;
</pre></p>
<p>From the shell start the REPL and in the REPL add a monitor which will load the immediateCompile module whenever the a source-file is changed.</p>
<p><pre class="brush: plain;">
mvn -Dyeti-compile=false clean compile yeti:repl
....
Ready to take your commands :)
yeti&gt;-s.addMonitor &quot;recompile monitor&quot; \(s.branche &quot;load example.immediateCompile&quot;);
&lt;p&gt;shell:added monitor 1: recompile monitor: &lt;code$&gt;
yeti&gt;
...
</pre></p>
<p>And now just modify in you editor the code of your module. Save it and see the compiliation output.</p>
<p>Ie introduce a bug:</p>
<p><pre class="brush: plain;">
module example.immediateCompile;

this is a bug

&quot;just an example showing the immediate compile function of the repl&quot;;
</pre></p>
<p>Save the file and you should (quite) immidiately see the compiler result on the repl.</p>
<p><pre class="brush: plain;">
yeti&gt;
yeti&gt;monitor:
yeti&gt;-----------------------
branche start: load example.immediateCompile
yeti&gt;load example.immediateCompile
example/immediateCompile.yeti:3:1: Unknown identifier: this
branche end: load example.immediateCompile
------------------------
yeti&gt;
</pre></p>
<p>In the repl you see that the code <code>load example.immediateResult</code> is executed and that the compiler showed the error: <code> example/immediateCompile.yeti:3:1: Unknown identifier: this</code>.</p>
<p>Correct the bug in your source file.</p>
<p><pre class="brush: plain;">
module example.immediateCompile;

&quot;this is no bug anymore&quot;
</pre></p>
<p>Save it and you get automatically on the repl</p>
<p><pre class="brush: plain;">
------------------------
yeti&gt;monitor:
yeti&gt;-----------------------
branche start: load example.immediateCompile
yeti&gt;load example.immediateCompile
&quot;this is no bug anymore&quot; is string
branche end: load example.immediateCompile
------------------------
</pre></p>
<p>The module was compiled, executed and returned <code>"this is no bug anymore" is string</code>. So everything works fine.</p>
<h2>Using the REPL to run Continously rudimentary Tests</h2>
<p>As we saw the monitor we added to load the module did not only compile the module it actually compiles and runs it too.</p>
<p>This can be of course used to also test modules as soon as you change them.</p>
<p>To do so instead of loading the main-source module you define a test module under <code>src/test/yeti</code> which in turn loads the main module and checks its result.</p>
<p>Lets do that:</p>
<p>Create a file src/test/yeti/example/testImmediateCompile.yeti:</p>
<p><pre class="brush: plain;">
module example.testImmediateCompile;

t = load example.immediateCompile;

if t != &quot;this is no bug anymore&quot; then failWith &quot;wrong result \(t)&quot; fi;

&quot;test immediateResult was ok&quot;
</pre></p>
<p>This module first loads the example.immediateCompile module than checks the returned string. If it does not fit it fails with an exception. Otherwise it finishes normally confirming that the module under test is ok.</p>
<p>Before we add a monitor for the new test module, we turn off the old monitor, using <code>-s.showMonitors() </code> to find its id and than calling <code>-s.removeMonitor id </code> to remove it.</p>
<p>Than we add the new monitor:</p>
<p><pre class="brush: plain;">
yeti&gt;-s.showMonitors()
shell:monitors:
1: true: recompile: &lt;code$&gt;

yeti&gt;-s.removeMonitor 1
shell:removed monitor 1
yeti&gt;
yeti&gt;-s.addMonitor &quot;testImmediateCompile&quot; \(s.branche &quot;load example.testImmediateCompile&quot;);
</pre></p>
<p>And now the module is not only recompiled each time you change and save it but also tested &#8211; very similar as in dynamic languages and even better than what the very advanced Java-IDEs provide.</p>
<p>This is of course no nice test-framework. A way to test yeti properly will be handled in a next blog-post.</p>
<p>But for having fun with Yeti it is a good start.</p>
<p>And if you are not board don&#8217;t forget to read the readme for the yeti-maven-plugin it has a few additional infos https://github.com/chrisichris/yeti-maven-plugin</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/chrisichris.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/chrisichris.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/chrisichris.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/chrisichris.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/chrisichris.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/chrisichris.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/chrisichris.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/chrisichris.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/chrisichris.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/chrisichris.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/chrisichris.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/chrisichris.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/chrisichris.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/chrisichris.wordpress.com/28/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chrisichris.wordpress.com&amp;blog=10115674&amp;post=28&amp;subd=chrisichris&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://chrisichris.wordpress.com/2011/02/25/the-yeti-maven-plugin-for-interactive-coding-and-running-yeti-code/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/02ec6ed574f3db2aa3c11a2637a2ea2c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">chrisichris</media:title>
		</media:content>
	</item>
		<item>
		<title>Yeti &#8211; probably the most agile of the statically typed languages on the JVM</title>
		<link>http://chrisichris.wordpress.com/2011/02/24/yeti-probably-the-most-dynamic-of-the-statically-typed-languages-on-the-jvm/</link>
		<comments>http://chrisichris.wordpress.com/2011/02/24/yeti-probably-the-most-dynamic-of-the-statically-typed-languages-on-the-jvm/#comments</comments>
		<pubDate>Thu, 24 Feb 2011 14:47:30 +0000</pubDate>
		<dc:creator>chrisichris</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Yeti]]></category>

		<guid isPermaLink="false">http://chrisichris.wordpress.com/?p=21</guid>
		<description><![CDATA[2 years ago I wanted to learn a new functional language on the the JVM. While I liked the simplicity of Clojure I chose Scala because of its static typechecking. I enjoyed working with scala but was on the latent search for something less baroque but with a good typesystem and typeinference. And than about a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chrisichris.wordpress.com&amp;blog=10115674&amp;post=21&amp;subd=chrisichris&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>2 years ago I wanted to learn a new functional language on the the JVM. While I liked the simplicity of Clojure I chose Scala because of its static typechecking. I enjoyed working with scala but was on the latent search for something less baroque but with a good typesystem and typeinference.</p>
<p>And than about a year ago I found by chance a link to yeti: <a href="http://linux.ee/~mzz/yeti/intro.html">http://linux.ee/~mzz/yeti/intro.html</a> Which was for me exactly the right mixture:</p>
<p>Yeti</p>
<ul>
<li>is a very simple yet powerful functional language</li>
<li>is staticly typed with full typeinference and with as little type-typing you can get &#8211; if you want so none like in a dynamic languge</li>
<li>runs on the JVM and integrates with java</li>
</ul>
<h2>Yeti is simple yet powerful</h2>
<p>In Yeti you have to know basicly 4 things:</p>
<ul>
<li>functions: because all code is in functions and invokes functions (there are no classes or objects)</li>
<li>values: 3 base types (number, string, unit ) and the composed types (list, array, hash, structure) and their literals ie ["one","two",three"] for a string list</li>
<li>how to assign the values to mutable and immutable variables</li>
<li>the if-expression and the case expression</li>
</ul>
<p>If you want to learn the whole language then read the intro <a href="http://linux.ee/~mzz/yeti/intro.html">http://linux.ee/~mzz/yeti/intro.html</a> Thats one document in a tutorial style which takes you about 2 hours to read and than you know 98 % of the language.</p>
<p>Yeti is also quite pragmatic it is not haskell strict immutable and you can typecast values work with objects etc.</p>
<p>Still yeti is powerful. Like in any functional language you can code in a very elegant concise way things which take much more LOC in a OO language.</p>
<p>Again please take a look at the tutorial I leave here the prove for later posts.</p>
<h2>Yeti has a simple but powerful type-system with full typeinference</h2>
<p>Any typesystem is a compromise: Either it is powerful and complicated and therefore can check a lot of different code or it is more simple and forbidds certain code which is otherwise perfectly legal. It is ongoing scientific process to find here a good balance.</p>
<p>However for many and for a long time now the ML typesystem und its typeinference alogartihmus is seen as a sweetspot in this compromise.</p>
<p>Yeti implements excatly this sweetspot-typesystem (with a few additions). To my knowledge no other VM-language does this.</p>
<p>The typesystem supports polymorphism (similar to Java Generics), mutable values and arrays with full type-inference.</p>
<p>As long as you don&#8217;t use legacy Java code you do not have to write any type in yeti &#8211; everything is inferred (you can if you want specify types on function etc for debugging &#8211; but you don&#8217;t have to).</p>
<p>In yeti you also do not define types. There are no classes, interfaces, traits etc. Yeti is purely structural typed. Every type is composed of the above mentioned basic types. The compiler inferece from your useage of values what you want and checks wheter you provide the right thing.</p>
<p>If your functions needs a structure with a field &#8220;swim&#8221; holding a function than the compiler checks wheter your code provides a structure with the field &#8220;swim&#8221; holding a function but it does not demand that you declare and instantiate a class &#8220;Duck&#8221; &#8220;Boat&#8221; or what soever etc.</p>
<p>So yeti is very very close to the &#8220;agile&#8221; dynamic languages, because it behaves like them in many respects (no typeannotations no typedelcarations no type instantiation) but it still checks the types, which saves a lot of test-code maintance and reading.</p>
<p>To my knowledge no other JVM language combines agility with static typechecking as close as Yeti.</p>
<h2>What is not so good with yeti?</h2>
<p>In my opinion the two main disadvantes are:</p>
<p>Yeti does not integrate as nicely with Java as Scala. But Java integration works both ways and with little problems. You just have to do a bit more work with types</p>
<p>Yeti does not have a big community. I thinkt thats its main problem</p>
<h2>Where to continue?</h2>
<p>Downlaod yeti <a href="http://mth.github.com/yeti/">http://mth.github.com/yeti/</a> read the tutorial <a href="http://linux.ee/~mzz/yeti/intro.html">http://linux.ee/~mzz/yeti/intro.html</a> and have fun with the repl.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/chrisichris.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/chrisichris.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/chrisichris.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/chrisichris.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/chrisichris.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/chrisichris.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/chrisichris.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/chrisichris.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/chrisichris.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/chrisichris.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/chrisichris.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/chrisichris.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/chrisichris.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/chrisichris.wordpress.com/21/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chrisichris.wordpress.com&amp;blog=10115674&amp;post=21&amp;subd=chrisichris&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://chrisichris.wordpress.com/2011/02/24/yeti-probably-the-most-dynamic-of-the-statically-typed-languages-on-the-jvm/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/02ec6ed574f3db2aa3c11a2637a2ea2c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">chrisichris</media:title>
		</media:content>
	</item>
	</channel>
</rss>
