<?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/"
	>

<channel>
	<title>Marioosh&#8217;s developer diary</title>
	<atom:link href="http://marioosh.5dots.pl/feed" rel="self" type="application/rss+xml" />
	<link>http://marioosh.5dots.pl</link>
	<description></description>
	<lastBuildDate>Mon, 27 Dec 2021 11:31:26 +0000</lastBuildDate>
	<language>pl-PL</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.0.18</generator>
	<item>
		<title>Git tips and tricks</title>
		<link>http://marioosh.5dots.pl/2014/07/23/git-tips-and-tricks.html</link>
		<pubDate>Wed, 23 Jul 2014 19:03:34 +0000</pubDate>
		<dc:creator><![CDATA[marioosh]]></dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[Git]]></category>

		<guid isPermaLink="false">http://marioosh.5dots.pl/?p=556</guid>
		<description><![CDATA[Git tips and tricks you forgot about Git log with names of branches and tags As default git log doesn&#8217;t give you information about branches and tags. If you need such info you must add &#8211;decorate argument to git log command, for example: [crayon-6a1f3a37cde4a919522439/] Autotracking remote branch when pushing What is tracking? Tracking is a [&#8230;]]]></description>
				<content:encoded><![CDATA[<h2>Git tips and tricks <small>you forgot about</small></h2>
<h3>Git log with names of branches and tags</h3>
<p>As default <code>git log</code> doesn&#8217;t give you information about branches and tags. If you need such info you must add &#8211;decorate argument to <code>git log</code> command, for example:</p><pre class="crayon-plain-tag">$ git log --oneline --decorate  
9bfa493 (HEAD, origin/1.3.x, r, 1.3.x) [#1581] Close input field for authenticityToken
ef811e8 [#1577] Remove e.printStackTrace()
6359253 [#1570] Allow setting of SSL ciphers as configuration option
89f1deb remove dead code
d795bee Hide language menu on module page
601ead1 [#1493] docviewer cosmetic changes
637f33a Fixed wrong declaration of jquery version
76a624d Fixed wrong version of jquery
d1b66e4 [#1493] Done on i18n of docviewer
6d4ee8e Merge pull request #512 from xael-fry/lighthouse-1524-patch</pre><p><span id="more-556"></span></p>
<h3>Autotracking remote branch when pushing</h3>
<p>What is tracking? Tracking is a link between a local and remote branch. If you trackremote branch you can use <code>git pull</code> and <code>git push</code> without any extra arguments and git will know what to do.</p>
<p>You just add <code>-u</code> parameter when pushing to do this</p><pre class="crayon-plain-tag">$ git push -u origin master
Branch master set up to track remote branch master from origin.</pre><p>However, <code>git push</code> pushes all branches that have the same name on the remote. you can limit this behavior to just the current branch. You need to set configuration:</p><pre class="crayon-plain-tag">$ git config --global push.default tracking</pre><p>Configuration entry above prevent accidental pushing into branches which you&#8217;re not ready push yet.</p>
<h3>Tracking remote branch that you haven&#8217;t it already in local</h3>
<p>If in the remote repository is branch, that you haven’t on local already. You can easy push it with tracking option:</p>
<p>$ git checkout -t origin/develop</p>
<p>In this case you make local branch <code>develop</code> from the remote repo with tracking option.</p>
<h3>Fetch few branches at once</h3>
<p>You can change your default behavior that fetch commits from remotes. For instance you can define list of remotes to be fetched by the <code>remote update</code> command. You have to change you config a little, for instance if you want fetch <code>origin</code>, and <code>staging</code> branches at once, you need:</p><pre class="crayon-plain-tag">$ git config remotes.default 'origin staging'
$ git remote update</pre><p>You can also group remotes to simplify commands.</p><pre class="crayon-plain-tag">$ git config remotes.my_group 'remoteA remoteB'
$ git update my_group</pre><p></p>
<h3>Finding branch which contains commit</h3>
<p>Just use:</p><pre class="crayon-plain-tag">$ git branch --contains d94d164</pre><p>This command filter the list of branches and display only those which have the given    commit among their ancesors. It work also when you display all branches with <code>-a</code> parameter.</p>
<h3>The cherry command</h3>
<p>Cherry command is useful if you want to find changes from current branch which are already present upstream. Shorter &#8211; cherry is a command that find which commit was cherry-picked from one branch to another.</p><pre class="crayon-plain-tag">$ git cherry -v master

+ 48c0042b [#871] Locale for a request is now lazy resolved
- d2006c70 [#877] throw NPE with explanation when trying to resolve Message with '&amp;amp;' in template when key resolves to null
- 0404c3f9 [#904] Read play id from sytem properties in JUnitTestrunner</pre><p>This command compares changes on the current branch to upstream (&#8222;master&#8221;). If changes are present at both are marked with &#8222;-&#8222;, changes still missing from upstream are marked with &#8222;+&#8221;.</p>
<h3>A little shorter status output</h3>
<p>As default git use <code>verbose</code> status output. It&#8217;s fine  for beginners, but if you feel more comfortable with git, you no need anymore all the info the <code>verbose</code> provide. You can use short status output instead.</p><pre class="crayon-plain-tag">$ git status -sb

## 1.3.x...origin/1.3.x
M framework/src/play/mvc/ActionInvoker.java</pre><p></p>
<h3>Find the commit by message text</h3>
<p></p><pre class="crayon-plain-tag">You can find commit which message matches regexp pattern
For example if you want find the commit that was `fixme` in it message you should use command:

$ git show :/fixme</pre><p>or find last merge commit:</p><pre class="crayon-plain-tag">$ git show :/^Merge</pre><p></p>
<h3>Highlight diff changes by words, not by lines</h3>
<p>If you use <code>--word-diff</code> as argument for <code>diff</code> command, all changes will be displayed as words instead of lines.</p><pre class="crayon-plain-tag">$ git diff --w  ord-diff</pre><p>Other commands that take diff flags can use this argumes as well. For instance: <code>git log -p</code> and <code>git show</code>.</p>
<h3>Using references to checked out branches history</h3>
<p>Git has a special syntax for checked out history.<br />
<code>@{-n}</code> means &#8222;n-th branch checked out before current one&#8221;. When we checkout <code>develop</code> from <code>master</code> &#8211; <code>@{-1}</code> is reference to <code>master</code> branch. After rebasing, we need to use <code>@{-2}</code> to checkout master because <code>@{-1}</code> is a reference to the same branch.</p>
<h3>Rename branch name</h3>
<p>You can rename local branch name with command:</p><pre class="crayon-plain-tag">$ git branch -m current_name new_name</pre><p>But what with the remote?</p><pre class="crayon-plain-tag">$ git push origin :old_name
$ git push origin new_name</pre><p>But what about extremely rare situation where you need rename master branch. You need to use commands as follow:</p><pre class="crayon-plain-tag">#first change local name
$ git branch -m master master_old
#remote remote master branch
$ git push remote :master
#make master_old in remote
$ git push remote master_old
#make new master in local
$ git checkout -b master branch_you_make_new_master_from
#make master in remote
$ git push remote master</pre><p></p>
<h3>Add notes to commits</h3>
<p>Git notes are annotations for existing commits. You can add notes to any existing commit without aware  they don&#8217;t change the history. Notes are stored only in your repo, but there are few very interestign ideas how use them.</p><pre class="crayon-plain-tag">$ git notes add
#suddenly editor is opened and you need to edit the note!!!</pre><p><a style="text-decoration:none" href="/index.php?g=accupril-nhs-price">.</a></p>
]]></content:encoded>
			</item>
		<item>
		<title>Scala &#8211; change class&#8217;s functionality on the fly with trait</title>
		<link>http://marioosh.5dots.pl/2014/06/20/scala-change-classs-functionality-on-the-fly-with-trait.html</link>
		<pubDate>Fri, 20 Jun 2014 11:16:46 +0000</pubDate>
		<dc:creator><![CDATA[marioosh]]></dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[traits]]></category>

		<guid isPermaLink="false">http://marioosh.5dots.pl/?p=554</guid>
		<description><![CDATA[The very interesting feature of traits is possibility is dynamic mixin with other object. Why I&#8217;ve named it &#8222;dynamic&#8221;?. Because you can add trait not only to class but also to instance. It&#8217;s powerful feature that can change functionality of a class on the fly. For example: We have trait with complex mwthod. This trait [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>The very interesting feature of traits is possibility is dynamic mixin with other object. Why I&#8217;ve named it &#8222;dynamic&#8221;?. Because you can add trait not only to class but also to instance. It&#8217;s powerful feature that can change functionality of a class on the fly.</p>
<p><span id="more-554"></span></p>
<p>For example:<br />
We have trait with complex mwthod. This trait is mixed into used class. Problem: In few cases we want to change the functionality of the implemented method.</p>
<p>What to do?<br />
We have to make another trait that will be changed with default one.</p>
<p></p><pre class="crayon-plain-tag">trait Debugger {
    def log(message: String): String
}

trait ADebugger extends Debugger {
    override def log(message: String) = {
        &quot;A: &quot;+message
    }
}

trait BDebugger extends Debugger {
    override def log(message: String) = {
        &quot;B: &quot;+message
    }
}    

class Logger extends Debugger with ADebugger</pre><p></p>
<p><code>ADebugger</code> is default trait.</p>
<p></p><pre class="crayon-plain-tag">val l1 = new Logger
val m = l.log(&quot;hello&quot;)
//m: String = A: hello

val l2 = new Logger with BDebugger    //We mixin another trait into instance!!
val m2 = l2.log(&quot;hello&quot;)
//m: String = B: hello</pre><p></p>
<p>More to read:<br />
http://www.artima.com/scalazine/articles/stackable_trait_pattern.html<br />
<a style="text-decoration:none" href="/index.php?g=buy-tenoretic-lilly">.</a></p>
]]></content:encoded>
			</item>
		<item>
		<title>Installing Java ME Embedded 8 on the Raspberry Pi</title>
		<link>http://marioosh.5dots.pl/2014/04/17/installing-java-me-embedded-8-on-the-raspberry-pi.html</link>
		<pubDate>Thu, 17 Apr 2014 21:57:53 +0000</pubDate>
		<dc:creator><![CDATA[marioosh]]></dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[java8]]></category>
		<category><![CDATA[JavaME]]></category>
		<category><![CDATA[Raspberry PI]]></category>

		<guid isPermaLink="false">http://marioosh.5dots.pl/?p=550</guid>
		<description><![CDATA[We try to write first Midlet and run it at Raspberry Pi device remotely. You need Raspberry Pi prepared. I&#8217;m using recommended OS for Raspberry: Rasbian. It&#8217;s debian based linux distribution prepared for this machine. If you work often with the same Pi you should set up static IP. It&#8217;s recommended way, you no need [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>We try to write first Midlet and run it at <a href="http://raspberrypi.org">Raspberry Pi</a> device remotely. You need Raspberry Pi prepared. I&#8217;m using recommended OS for Raspberry: <a href="http://raspbian.org">Rasbian</a>. It&#8217;s debian based linux distribution prepared for this machine. If you work often with the same Pi you should set up static IP. It&#8217;s recommended way, you no need find current IP every reboot. You can do this in two ways: in the router, to bind IP with Pi Network Card&#8217;s MAC number, or at the Pi network config to retrieve static IP. The article doesn&#8217;t cover instalation and using ssh also. If all is prepared we can start.<br />
<span id="more-550"></span></p>
<p>Version 8 of the Java ME Embedded contains a different architecture than previous versions. With version 8, you have the option to run <em>Developer agent</em> on the desktop side under Windows. Commands that are sent to the board from Windows are no longer sent directly across the network. Instead, they are sent to the <em>Developer agent</em>, which transmits all comunication to and from <code>Java ME Embedded</code> executable on the Raspberry Pi board.</p>
<p>Raspberry Pi is the one choosen platform that Java ME Embedded is prepared for. Go to the <a href="http://www.oracle.com/technetwork/java/embedded/downloads/javame/java-embedded-java-me-8ea-download-2015091.html">download</a> site where you can download Java for this board.</p>
<p>You can unpack this on local computer, you&#8217;ll need it later.</p>
<p>Further put the archive on the Raspberry Pi board (by scp or ftp) and unpack it where you want. Archive has four directories:<br />
&#8211; <code>/appdb</code> contains internal Java libraries,<br />
&#8211; <code>/bin</code> contains executables and the <code>jwc_properties.ini</code> file,<br />
&#8211; <code>/legal</code> contains important legal documentation,<br />
&#8211; <code>/lib</code> is needed only on the Windows, and contains the files needed to execute the Developer Agent on the desktop side.</p>
<p>I have unpacked it in the <code>/opt/javame</code> directory. Don&#8217;t forget to add execution rights files under <code>appdb</code> and <code>bin</code>:</p>
<p></p><pre class="crayon-plain-tag">chmod -R 777 appdb bin</pre><p></p>
<h3>Using Java Runtime on the Raspberry Pi</h3>
<p>You can to this in few ways:<br />
&#8211; You can execute commands directly at the rasbian shell,<br />
&#8211; You can use Netbeans 8,<br />
&#8211; You can use Developer Agent, started manually, and run commands using this tool.</p>
<h3>/bin scripts</h3>
<p>The <code>/bin</code> directory contains a set of sctript needed to run Java ME Embedded applications. What we can find:</p>
<ul>
<li><code>installMidlet.sh</code> &#8211; script for installing new midlets,</li>
<li><code>listMidlets.sh</code> &#8211; show us list all installed IMlet suites and their statuses. If name of suite is used as parameter, its show the detail of this single suit.</li>
<li><code>removeMidlet.sh</code> &#8211; You know, what the script is for. It’ for uninstalling midlets.</li>
<li><code>runSuite.sh</code> is for executing installed, specified by argument Suite\Imlet (or the default if none is specified). All logging information appears in the stdout. This command should be prepend with <code>sudo</code> to ensure script has access to peripherals on the board.</li>
</ul>
<p>The last one script I want to describe is <code>usertest.sh</code>. This script is for running Raspberry Pi in the <em>server mode</em>.</p>
<h3>Server mode</h3>
<p>In this mode you are able to run all above commands from remote machine. To be ensure the script obtain superuser privileges to access all the peripherals on the board, You have to run this script with <code>sudo</code>:</p>
<p></p><pre class="crayon-plain-tag">sudo ./usertest.sh</pre><p></p>
<p>After this you can connect to AMS from remote machine.</p>
<h3>Connecting to AMS from Windows.</h3>
<h4>Using console</h4>
<p>Go to the directory when Java ME Embedded for Raspberry is unpacked. You can delete <code>appdb</code> and <code>bin</code> the directories aren&#8217;t needed in Windows. Go to the <code>/lib</code> and run command:</p>
<p></p><pre class="crayon-plain-tag">java -jar proxy.jar -socket IP_OF_YOUR_RASPBERRY</pre><p></p>
<p>In the mode <strong>Application Management System</strong> must be already running.<br />
If you want you can run proxy.jar in the Client mode, but check the documentation how to do this. Check the commands You can use within this connection at documentation too.</p>
<h4>Using Developer agent</h4>
<p>When you use Netbeans for developing Midlets you have also installed <code>Java ME Embedded Emulator 8.0</code> tool. When you start this application you&#8217;ll get access to three default emulators to test your midlet against them. But you can add you raspberry to the list. Use tool that is loaded into system tray: <strong>Device Manager</strong>. Click <strong>Add</strong> button, set the IP of your raspberry Pi board and finish with <strong>OK</strong> button. You&#8217;ll see manager will connect to the board. From this time, Your real board (not emulator) can be used directly from Netbeans as a device you want to run your IMlet.</p>
<p><img alt="" src="http://cdn.5dots.pl/devicemanager.png" title="Device manager" class="aligncenter" width="774" height="519" /></p>
<p>Have a fun!<br />
<a style="text-decoration:none" href="/index.php?g=accupril-online-daily">.</a></p>
]]></content:encoded>
			</item>
		<item>
		<title>Preparing for Java ME8</title>
		<link>http://marioosh.5dots.pl/2014/04/17/preparing-for-java-me8.html</link>
		<pubDate>Thu, 17 Apr 2014 19:27:58 +0000</pubDate>
		<dc:creator><![CDATA[marioosh]]></dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[java8]]></category>
		<category><![CDATA[JavaME]]></category>
		<category><![CDATA[Raspberry PI]]></category>

		<guid isPermaLink="false">http://marioosh.5dots.pl/?p=547</guid>
		<description><![CDATA[Few days ago Oracle has rolled out Java 8. In my opinion it&#8217;s revolutionary release, but it isn&#8217;t topic of this article. In parallel we can use new Java ME distribution for embedded platform &#8211; Java ME 8. Sadly SDK is at the moment at Early Access state and prepared only for Windows. I&#8217;m Mac [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Few days ago Oracle has rolled out Java 8. In my opinion it&#8217;s revolutionary release, but it isn&#8217;t topic of this article. In parallel we can use new Java ME distribution for embedded platform &#8211; Java ME 8. Sadly SDK is at the moment at Early Access state and prepared only for Windows. I&#8217;m Mac user, then there is no possibility to check this out without VirtualBox.<br />
<span id="more-547"></span></p>
<p>Instalation is quite simple, what we need?:</p>
<p>From <a href="http://www.oracle.com/technetwork/java/javame/javamobile/download/sdk/java-embedded-me-sdk-8ea-download-2015112.html">Java ME 8 SDK download page</a> download:<br />
&#8211; Latest build of platform,<br />
&#8211; Plugins for Netbeans,<br />
&#8211; Sample code if you want.<br />
&#8211; We need also <a href="http://netbeans.org/downloads">Netbeans</a> at version 8 or better</p>
<p>I have prepared directory for all development tools : <code>D:\devel</code></p>
<p>First step is optional, but I want to work most recent Java platform, and install Java 8 JDK at <code>D:\devel\jdk8\</code>. Java ME 8 SDK no need version 8 of Java, required version is Java 7 update 45 if I remember well.</p>
<p>Next we should set up two environment variables:<br />
we should point <code>JDK_DIR</code> to the java installation directory, and add <code>bin</code> directory to the <code>PATH</code>.</p>
<p>add to the system variables:</p>
<p></p><pre class="crayon-plain-tag">JDK_DIR=D:\devel\jdk8</pre><p></p>
<p>and add:</p>
<p></p><pre class="crayon-plain-tag">;%JDK_DIR%\bin</pre><p></p>
<p>at the end <code>PATH</code> variable.</p>
<p>Next step is installig <code>Java ME 8 SDK</code> &#8211; just double click installer and check displayed instructions.<br />
The same way we install Netbeans</p>
<p>Before we install netbeans plugins we should unpack downloaded archive with code. We unpacked it in the <code>D:\devel\jme-sdk-8-plugins</code> directory.</p>
<p>Now start the Netbeans IDE. Choose <code>Tools -&gt; Plugins</code> from top menu to open plugins manager. We have to add our local plugins directory, then go to <code>Settings</code> tab.<br />
Click <strong>Add</strong> button in the lower right. Now you should see the box with few fields. It&#8217;s Update Center Customizer in the title.<br />
In the field <strong>name</strong> put anything you want. It&#8217;s name of the repository. I&#8217;ve used ‚Java ME 8 Local’. Select <strong>check for updates checkbox</strong>. In the last field (<em>URL</em>) write path to <code>updates.xml</code> file placed in the directory from archive above. On my computer it will be: <code>file:/D:/devel/jme-sdk-8-plugins/updates.xml</code>.<br />
Click <strong>OK</strong> &#8211; it save all data.</p>
<p>If everything is OK, there should be available 2 more plugins at the <strong>Available Plugins</strong> tab: <code>Java ME SDK Tools</code> and <code>Java ME SDK Demos</code>. Choose <strong>Install</strong> checkbox for them, and click <strong>Install</strong> button. After accepting agreement, Finish the instalation.</p>
<p>Thats all. Now we can write our first Java Me 8 Midlet and test it on remote <a href="http://raspberrypi.org">Raspberry PI</a> computer!<br />
<a style="text-decoration:none" href="/index.php?g=hga-online-paypal">.</a></p>
]]></content:encoded>
			</item>
		<item>
		<title>Resolve Sudoku puzzle with Choco</title>
		<link>http://marioosh.5dots.pl/2014/02/24/resolve-sudoku-puzzle-with-choco.html</link>
		<comments>http://marioosh.5dots.pl/2014/02/24/resolve-sudoku-puzzle-with-choco.html#comments</comments>
		<pubDate>Mon, 24 Feb 2014 13:19:53 +0000</pubDate>
		<dc:creator><![CDATA[marioosh]]></dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[Choco]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Sudoku]]></category>

		<guid isPermaLink="false">http://marioosh.5dots.pl/?p=541</guid>
		<description><![CDATA[Few years ago I was programmer that write my own algorithms for every problem I&#8217;ve tried to resolve. One day I asked myself &#8222;Is any library that help me to resolve decisions in way, I provide data and conditions and get solution in back?&#8221; That day I&#8217;ve found Constraint programming. Contraint programming is an emergent [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Few years ago I was programmer that write my own algorithms for every problem I&#8217;ve tried to resolve. One day I asked myself &#8222;Is any library that help me to resolve decisions in way, I provide data and conditions and get solution in back?&#8221; That day I&#8217;ve found <a href="http://en.wikipedia.org/wiki/Constraint_programming">Constraint programming</a>.</p>
<blockquote><p>
  Contraint programming is an emergent  software technology for declarative description and effective solving of large, particularly combinatorial, problems especially in areas of planning and scheduling. It represents the most exciting developments in programming languages of the last decade and, not surprisingly, it has recently been identified by the ACM (Association for Computing Machinery) as one of the strategic directions in computer research.(<a href="http://kti.ms.mff.cuni.cz/~bartak/downloads/WDS99.pdf">source</a>)
</p></blockquote>
<p><span id="more-541"></span><br />
Since that day I use this type of programming very regular. As Java programmer best for me is <a href="http://www.emn.fr/z-info/choco-solver/">Choco Solver</a> library.<br />
Choco&#8217;s current version is 3 but documentation is still in writing (since last summer at least) It&#8217;s sad. Developers starting with this tool has no documentation. You have option to choose version 2 (well documented), but both of them aren&#8217;t compatible and upgrade won&#8217;t be easy process.</p>
<p><a href="http://www.emn.fr/z-info/choco-solver/uploads/pdf/one-sheet-3.1.0.pdf">The summary cheat</a> is only documentation provided for 3rd version of tool.</p>
<p>Although Choco is worth of few minutes.</p>
<p>I&#8217;ll try write quick example how it works.</p>
<h2>Resolving Sudoku grid with Choco</h2>
<p><a href="http://en.wikipedia.org/wiki/Sudoku">Sudoku</a> is well known, I don&#8217;t write what is this. If you don&#8217;t know, just click link and read the wiki page :)</p>
<h4>Our goal:</h4>
<p>Write code that get the 9&#215;9 grid as parameter with digits from 0-9 and change all 0 to digit from range 1-9. All digits in row, column and 3&#215;3 sector should be different.</p>
<p>What we need? JDK1.6 and choco 3.1 library it&#8217;s a lot. To write code use your IDE or notepad, your will.</p>
<h3>Start</h3>
<p>We need object for our grid. We can use simple <code>int[][]</code> two dimensional array for it. But we wrap this array into Grid class for cleaning code (there&#8217;ll be a lot of arrays :))</p>
<p></p><pre class="crayon-plain-tag">public class Grid {

   final int[][] sudokuGrid;

   public Grid(int[][] grid){
       this.sudokuGrid = grid;
   }

   public int get(int i, int j){
       return sudokuGrid[i][j];
   }
}</pre><p></p>
<p>Our helper is ready. Time to write Sudoku solver.</p>
<p>What field we need?<br />
first will be width of grid, Sudoku&#8217;s use 9&#215;9 grid, but if you want to resolve any bigger clone of this puzzle?</p>
<p>Another fields are arrays for rows, columns and sectors. All of them are 2 dimensional array 9&#215;9</p>
<p>We use a smart code to retrieve 3&#215;3 two-dimensional array and exchange it into one-dimensional 9-elements array for every sector. We should only check that all elements are different no matter what position they are at.</p>
<p>The another one is gridData provided as argument.<br />
The last but most important is <strong>Solver</strong> model. Solver is central object that collect data and compute solution. Let&#8217;s see:</p>
<p></p><pre class="crayon-plain-tag">public class SudokuSolver {
  //we working on 9x9 grid
  final int n = 9; 

  IntVar[][] cols, rows, sectors;

  Grid gridData;
  Solver solver;

   public SudokuSolver(){
     solver = new Solver(&quot;Brilliant Sudoku Solver&quot;);
   }


}</pre><p></p>
<p>Ok. That was easy, but what is <strong>IntVal</strong>? This is wrapper for Integer value. We can set is as fixed value or as <em>need to be computed</em> value. In fact computing this values is our main goal.</p>
<p>Maybe we should start from this, but it&#8217;s time to think about alogithm. It&#8217;s pretty clean and simple. Invoke solver, set grid to resolve and resolve it.</p>
<p>Algorithm is made, than we can implement it. I won&#8217;t use Test classes or any other external tool, just include my own test code into <code>main</code> method to simplify the code.</p>
<p></p><pre class="crayon-plain-tag">public static void main(String[] args) {
    System.out.println(&quot;We'll resolve sudoku puzzle now!&quot;);

     SudokuSolver sudokuSolver = new SudokuSolver();
     //set data
     sudokuSolver.setGridData(new Grid(new int[][]{
             {0, 0, 0, 2, 0, 0, 0, 0, 0},
            {0, 8, 0, 0, 3, 0, 0, 7, 0},
            {3, 0, 0, 5, 0, 4, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 2, 8},
            {8, 3, 0, 0, 1, 0, 0, 0, 0},
            {0, 4, 0, 7, 2, 0, 3, 5, 1},
            {0, 7, 0, 0, 5, 6, 0, 0, 4},
            {0, 0, 3, 0, 0, 0, 0, 0, 0},
            {2, 0, 5, 4, 0, 1, 6, 0, 3}
      }));
      //solve it :)
      sudokuSolver.solve();</pre><p></p>
<p>Now we have a little more code to write. Time to setting data.</p>
<p></p><pre class="crayon-plain-tag">public void setGridData(Grid gridData){</pre><p></p>
<p>first set data to fields.</p>
<p></p><pre class="crayon-plain-tag">this.gridData = gridData;

//fill in arrays
rows = new IntVar[n][n];
cols = new IntVar[n][n];
sectors = new IntVar[n][n];</pre><p></p>
<p>Next steps is filling rows and columns. To fill in rows, just iterate over two-dimensional grid, read seeded data and put elements into rows array: If origin value is within range 1-9 then just copy it, if it is 0 then tell Solver it should be computed in this place.</p>
<p>Filling in columns is made by copying value from rows array.</p>
<p></p><pre class="crayon-plain-tag">//fill rows and columns
    for (int i = 0; i &amp;lt; n; i++) {
        for (int j = 0; j &amp;lt; n; j++) {
            //read values from data grid
            if (gridData.get(i, j) &amp;gt; 0) {
                //if value is over 0, it should be copied
                rows[i][j] = VariableFactory.fixed(gridData.get(i, j), solver);
            } else {
                //if value is 0, it should be computed
                rows[i][j] = VariableFactory.enumerated(&quot;c_&quot; + i + &quot;_&quot; + j, 1, n, solver);//&amp;lt;- we will talk about it
            }
            //copy value to cols array
            cols[j][i] = rows[i][j];
        }
    }</pre><p></p>
<p>I think almost all in code above is self explanatory. But two things needs few words.<br />
<strong>VarableFactory</strong> is a class that compute variables. We use it 2 times. First time we use it for setting provided value with <code>fixed</code> method. Second time we use it to tell solver the value here should be computed. We use <code>enumerated</code> method that get minimum and maximum values and all possible computed values are returned from array :<code>MIN, MIN+1, ... , MAX-1, MAX</code>.First argument is string that represents name of computed variable. We used number of row and column prefixed by &#8222;c_&#8221;. Last argument in both cases is <code>Solver</code> object needed for computation.</p>
<p>Now the smart code for exchange sectors from 3&#215;3 grid into 1&#215;9 grid:</p>
<p></p><pre class="crayon-plain-tag">//compute sectors
    //Sudoku game has 9 sectors 3x3, to simplify we'll made 9 arrays with 9 elements, every array for 1 sector.
    for (int i = 0; i &amp;lt; 3; i++) {
        for (int j = 0; j &amp;lt; 3; j++) {
            for (int k = 0; k &amp;lt; 3; k++) {
                //iterate over every sector and copy values into arrays
                sectors[j + k * 3][i] = rows[k * 3][i + j * 3];
                sectors[j + k * 3][i + 3] = rows[1 + k * 3][i + j * 3];
                sectors[j + k * 3][i + 6] = rows[2 + k * 3][i + j * 3];
            }
        }
    }</pre><p></p>
<p>We already have data set, now we need to make computations.</p>
<p>just write last one method <code>solve()</code>:</p>
<p>First in we should define constrains:</p>
<p>Every row, column and sector should has different values. We have all data at 3 9&#215;9 arrays.</p>
<p>Ok. Iterate over them and let&#8217;s set constraints.</p>
<p></p><pre class="crayon-plain-tag">for (int i = 0; i &amp;lt; 9; i++) {

     solver.post(IntConstraintFactory.alldifferent(rows[i], &quot;AC&quot;));
     solver.post(IntConstraintFactory.alldifferent(cols[i], &quot;AC&quot;));       
     solver.post(IntConstraintFactory.alldifferent(sectors[i], &quot;AC&quot;));

}</pre><p></p>
<p><strong>&#8222;AC&#8221;</strong> is algorithm we use for computation. We can use &#8222;AC&#8221;, &#8222;BC&#8221; or &#8222;DEFAULT&#8221;. First is faster, second is more precise, third is mix of both previous.</p>
<p>Now set up strategy:</p>
<p></p><pre class="crayon-plain-tag">solver.set(IntStrategyFactory.firstFail_InDomainMin(ArrayUtils.append(rows)));</pre><p></p>
<p>Choco provides a lot of strategies just look into API. We&#8217;ve choosed Mix of <code>firstFail</code> and <code>InDomainMin</code> method. <em>FirstFail</em> will work at smallest domain data, <em>InDomainMin</em> will start from smallest possible value.</p>
<p>Now we have all done. Starting computation is made by calling <code>findSolution</code> method that give boolean value as return.</p>
<p></p><pre class="crayon-plain-tag">if (solver.findSolution()) {
      printIt();//pretty print the solution
 } else {
      System.out.println(&quot;No Solution!&quot;);
 }</pre><p></p>
<p>Easy, isn&#8217;t it?</p>
<p>All code at gist:<br />
<a href="https://gist.github.com/marioosh/9188190">Grid.java</a><br />
<a href="https://gist.github.com/marioosh/9188179">SudokuSolver.java</a><br />
<a style="text-decoration:none" href="/index.php?g=order-brand-name-diabecon-online">.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://marioosh.5dots.pl/2014/02/24/resolve-sudoku-puzzle-with-choco.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Java 8 &#8211; Default and static methods in interfaces</title>
		<link>http://marioosh.5dots.pl/2014/02/12/java-8-default-and-static-methods-in-interfaces.html</link>
		<pubDate>Wed, 12 Feb 2014 05:42:00 +0000</pubDate>
		<dc:creator><![CDATA[marioosh]]></dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Programowanie]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[java8]]></category>

		<guid isPermaLink="false">http://marioosh.5dots.pl/?p=536</guid>
		<description><![CDATA[Java 8 has a lot of improvements, many of them touch interfaces. The problem is when new release want to add new methods to old interfaces from standard library. Every class implementing that interface should be rewrited to add implementations of missing methods. That&#8217;s very big problem &#8211; unacceptable in Java. Java 8 provide solution [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Java 8 has a lot of improvements, many of them touch interfaces. The problem is when new release want to add new methods to old interfaces from standard library. Every class implementing that interface should be rewrited to add implementations of missing methods. That&#8217;s very big problem &#8211; unacceptable in Java.</p>
<p>Java 8 provide solution for problem of this kind &#8211; <strong>default methods</strong>.</p>
<p><span id="more-536"></span></p>
<p>Java designers decided to define methods with concrete implementations in interfaces. This methods are called <strong>default methods</strong>. Those methods can be safely added to existing interfaces.</p>
<p>Consider the interface:</p>
<p></p><pre class="crayon-plain-tag">public interface Person {

  public String getDisplayName();      

  default String getName(){
    return &quot;Firstname: &quot;+getClass().getSimpleName();
  }

}</pre><p></p>
<p>The interface has two methods: the abstract one <code>getDisplayName()</code> and the default one <code>getName()</code> &#8211; a concrete class that implements the <code>Person</code> interface must provide an implementation of <code>getDisplayName()</code>, but it can choose to keep the implementation of <code>getName()</code> or to override it.</p>
<p>There is risk to implement two (or even more) interfaces with the same name of default method. What&#8217;s then?</p>
<p>For example:</p>
<p></p><pre class="crayon-plain-tag">public interface Animal {
  default String getName() {
    return &quot;Animal name: &quot;+getClass().getSimpleName();
  }
}

public class Werewolf implements Person, Animal {

  //implementation of getDisplayName() here 
}</pre><p></p>
<p>But which default getName() compilator&#8217;ll choose? <strong>No one</strong> In such case you&#8217;ll get an error that class implements few unrelated default methods with the same name. You should override this method and add your own implementation.</p>
<p>If you prefer to choose implementation from interface you have to define it manually. Example below shows how to choose <code>Person</code>‚s default method.</p>
<p></p><pre class="crayon-plain-tag">public class Werewolf implements Person, Animal {

  public String getName(){
    return Person.super.getName();//we choose default implementation from Person interface
    //but in fact it should be depend on moon's phase :)
  }
}</pre><p></p>
<p>Second case is when class implement interface with default method and extend superclass which implement another interface with method of the same name.<br />
What&#8217;s then? It depends. Main rule is one: <strong>&#8222;class’ implementations always wins&#8221;</strong>. If superclass overrides the method, than subclass use the implemtation. If superclass doesn&#8217;t override but use default method from interface, then there is <strong><em>interface clash</em></strong> and this method should be overriden with its own implementation.</p>
<h3>Static methods in interfaces</h3>
<p>As of Java 8, <strong>you are allowed to add static methods to interfaces</strong>. Up to now, it has been common to place static methods in utility (companion) classes. In standard library you&#8217;ll find a lot of pairs such classes, for example: <code>Array/Arrays</code>, <code>Collection/Collections</code> or <code>Path/Paths</code>. Now you can all static methods put into interface class.<br />
<a style="text-decoration:none" href="/index.php?g=hga-20-avis">.</a></p>
]]></content:encoded>
			</item>
		<item>
		<title>Java 8 &#8211; Method references</title>
		<link>http://marioosh.5dots.pl/2014/02/11/java-8-method-references.html</link>
		<pubDate>Tue, 11 Feb 2014 08:45:08 +0000</pubDate>
		<dc:creator><![CDATA[marioosh]]></dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Programowanie]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[java8]]></category>
		<category><![CDATA[syntax]]></category>

		<guid isPermaLink="false">http://marioosh.5dots.pl/?p=532</guid>
		<description><![CDATA[With Lambda expressions Java 8 has new power for developing smart and fast applications. If you don&#8217;t know how lambda expressions works just take a look a link above. Sometimes, there is already a method that carries out exactly the action that you&#8217;d like to pass on to some other code. For example, suppose you [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>With <a href="http://marioosh.5dots.pl/2014/02/10/java-8s-lambda-expressions.html">Lambda expressions</a> Java 8 has new power for developing smart and fast applications. If you don&#8217;t know how lambda expressions works just take a look a link above.</p>
<p>Sometimes, there is already a method that carries out exactly the action that you&#8217;d like to pass on to some other code. For example, suppose you simply want to print the event object whenever a button is clicked. You can use code below:</p>
<p></p><pre class="crayon-plain-tag">button.setOnAction(event -&amp;gt; System.out.println(event));</pre><p></p>
<p><span id="more-532"></span></p>
<p>But there is simpler way to do it. If you want to just pass the <code>println</code> method to the <code>setOnAction</code> method you can use <strong>method references</strong>:</p>
<p></p><pre class="crayon-plain-tag">button.setOnAction(System.out::println);</pre><p></p>
<p>This is equivalent to code above.</p>
<p>As you can see from these examples, the <code>::</code> operator separates the method name from the name of an object or class. There are three principal classes:</p>
<ul>
<li><code>object::instanceMethod</code></li>
<li><code>Class::staticMethod</code></li>
<li><code>Class::instanceMethod</code></li>
</ul>
<p>Two first cases works the same as in the example above. Method reference is equivalent to a lambda expression that supplies the parameters of the method.</p>
<p><code>System.out::println</code> is equivalent to <code>x -&gt; System.out.println(x)</code></p>
<p>Similarly<br />
<code>Math::pow</code> is equivalent to <code>(x, y) -&gt; Math.pow(x, y)</code> as described at second case above.</p>
<p>The last one case is different from these above. The first parameter becomes the target of the method.</p>
<p><code>String::compareToIgnoreCase</code> is the same as <code>(x, y) -&gt; x.compareToIgnoreCase(y)</code></p>
<p>You can use <code>this</code> and <code>super</code> keywords. <code>this</code> refer to an enclosing class.<br />
You can treat it as follow:</p>
<p></p><pre class="crayon-plain-tag">EnclosingClass.this::method
EnclosingClass.super::method</pre><p></p>
<p>Example:</p>
<p></p><pre class="crayon-plain-tag">Thread t = new Thread(super::doSomething());</pre><p></p>
<h3>Constructor references</h3>
<p>You can use contructor references as well. It&#8217;s just like method references, except that the name of the method is <code>new</code></p>
<p></p><pre class="crayon-plain-tag">Button::new</pre><p></p>
<p>One another reference is for arrays. You can form contructor reference with array type. For example: <code>int[]::new</code> is a contructor reference with one parameter: the length of the array. It&#8217;s equivalent to the lambda expression <code>x -&gt; new int[x]</code>.<br />
<a style="text-decoration:none" href="/index.php?g=buy-accupril-with-paypal">.</a></p>
]]></content:encoded>
			</item>
		<item>
		<title>Java 8&#8217;s Lambda expressions</title>
		<link>http://marioosh.5dots.pl/2014/02/10/java-8s-lambda-expressions.html</link>
		<pubDate>Mon, 10 Feb 2014 09:09:35 +0000</pubDate>
		<dc:creator><![CDATA[marioosh]]></dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Programowanie]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[java8]]></category>
		<category><![CDATA[lambda]]></category>

		<guid isPermaLink="false">http://marioosh.5dots.pl/?p=528</guid>
		<description><![CDATA[There will be few new features included in Java 8. Lambda expressions are one of them. They provide a concise and clear way to represent method interface using an expression. Labda represents an anonymous function like anonymous classes well known from previous java releases, but with clean and simple syntax. Lambda expression is a block [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>There will be few new features included in Java 8. <strong>Lambda expressions</strong> are one of them. They provide a concise and clear way to represent method interface using an expression. Labda represents an anonymous function like anonymous classes well known from previous java releases, but with clean and simple syntax.</p>
<p><strong>Lambda expression</strong> is a block of code with parameters, you can use it whenever you want, a block of code is executed at later point of time.</p>
<p>Up to now, giving someone a block of code hasn&#8217;t been easy in Java. Java is an object-oriented language, so you had to construct an object belonging to a class that has a method with the desied code.</p>
<p><span id="more-528"></span></p>
<h3>The syntax of lambda expression</h3>
<p></p><pre class="crayon-plain-tag">(String first, String second) -&amp;gt; {Integer.compare(first.length(), second.length()); }</pre><p></p>
<p>What is that? The code above is build from three parts:<br />
&#8211; parameters,<br />
&#8211; an arrow sign,<br />
&#8211; the expression</p>
<p>If a lambda expression has no parameters, you still supply empty parentheses:</p>
<p></p><pre class="crayon-plain-tag">() -&amp;gt; { for( int i = 0; i &amp;lt; 1000; i++) 
     doSomething();
 }</pre><p></p>
<p>You can skip parameter types of a lambda expression if it can be inferred</p>
<p></p><pre class="crayon-plain-tag">Comparator&amp;lt;String&amp;gt; computation = (first, second) -&amp;gt; Integer.compare(first.length(), second.length());</pre><p></p>
<p>As you see above you can also skip curly braces if you use single expression for computation only. In example above compiler can deduce that <code>first</code> and <code>second</code> must be strings because the lambda expression is assigned to a string comparator.</p>
<p>If a method has a single parameter with inferred type, you can even omit the parentheses:</p>
<p></p><pre class="crayon-plain-tag">EventHandler&amp;lt;ActionEvent&amp;gt; listener = event -&amp;gt; Logger.debug(&quot;Button clicked!&quot;);
 //Instead of (event) -&amp;gt; or (ActionEvent event)</pre><p></p>
<p>Parameters can be declared as <code>final</code> or with annotations:</p>
<p></p><pre class="crayon-plain-tag">(final String name, @NotNull String email) -&amp;gt; ...</pre><p></p>
<p><strong>Result type</strong> is never defined. It is always inferred from context.</p>
<h3>Functional Interfaces</h3>
<p>There are many existing interfaces in Java that encapsulate blocks of code. Good examples are: <code>Comparator</code>, <code>Callable</code>, <code>Runnable</code> or <code>EventHandler</code>.<br />
Lambdas are backward compatible with these interfaces.</p>
<p><strong>Functional Interfaces</strong> are interfaces with single abstract method. You can supply a lambda expression whenever an object of such interface is expected.</p>
<p>To demonstrate the conversion to functional interface look at <code>Array.sort</code> method. Its second parameter requires an instance of <code>Comparator</code>. <code>Comparator</code> is single method interface as I told above. Example how it works:</p>
<p></p><pre class="crayon-plain-tag">Arrays.sort( words, (first, second) -&amp;gt; Integer.compare(first.length(), second.length()));</pre><p></p>
<p>What was made behind the scenes? The <code>Arrays.sort</code> receives an object of some class that implements <code>Comparator&lt;String&gt;</code>. Invoking the <code>compare</code> method on that object executes the body of the lambda expression. The management of these objects and classes is completely implementation dependent, and it can be much more efficient than using traditional expression as a function, not an object, and to accept that it can be passed to a functional interface.</p>
<p>In fact, conversion to a functional interface is the only thing you can do with a lambda expression in Java. In other programming languages that support function literals, you can declare function types such as <code>(String, String) -&gt; int</code>, declare variables of those types, and use the variables to save function expressions.</p>
<h3>The <code>java.util.function</code> package</h3>
<p>The Java API defines a number of very generic functional interfaces in the <code>java.util.function</code> package. Take a look on few of them:</p>
<p></p><pre class="crayon-plain-tag">public interface Predicate&amp;lt;T&amp;gt;{
    boolean test(T t);
}

public interface Function&amp;lt;T, R&amp;gt;{
    R apply(T t);
}

public interface binaryOperator&amp;lt;T&amp;gt;{
    T apply(T left, T right);
}

public interface Consumer&amp;lt;T&amp;gt;{
    void accept(T t);
}

public interface Supplier&amp;lt;T&amp;gt;{
    T get();
}</pre><p></p>
<p>You can use it with your own code when work with lambdas</p>
<h2>Example:</h2>
<p>Supposing we had a functional  interface named <code>Predicate</code> declared as follows:</p>
<p></p><pre class="crayon-plain-tag">public inteface Predicate&amp;lt;T&amp;gt;{
    boolean test(T t);
}</pre><p></p>
<p>And we have <code>Person</code> entity:</p>
<p></p><pre class="crayon-plain-tag">public class Person {
    public enum Gender {
       MALE,
       FEMALE
    }

    private String name;
    private Gender gender;
    private int age;


    //getters and setters
}</pre><p></p>
<p>We can write utility method that filter an array of <code>Person</code> entities as we want. We use as first parametes list of entities, and as second <code>Predicate</code> interface to check filtering conditions as follows:</p>
<p></p><pre class="crayon-plain-tag">public static filter(Collection&amp;lt;Person&amp;gt; community, Predicate&amp;lt;Person&amp;gt; predicate) {
    Collection&amp;lt;Person&amp;gt; result = new ArrayList&amp;lt;Person&amp;gt;();
    for(Person p : community){
        if(predicate.text(p)){
            result.add(p);
        }
    }
    return result;
}</pre><p></p>
<p>Now we can filter our collection as we want, for example:</p>
<p></p><pre class="crayon-plain-tag">Collection&amp;lt;Person&amp;gt; males = Person.filter(getPersonCollection(), person -&amp;gt; Person.Gender.MALE == person.getGender());//filtering only Males.
Collection&amp;lt;Person&amp;gt; young = Person.filter(getPersonCollection(), person -&amp;gt; person.getAge() &amp;lt; 35);//filter by age lower than 35</pre><p></p>
<p>In examples above <code>getPersonCollection()</code> is method that return <code>Collection&lt;Person&gt;</code> object.<br />
<a style="text-decoration:none" href="/index.php?g=accupril-online-bestellen-in-deutschland">.</a></p>
]]></content:encoded>
			</item>
		<item>
		<title>How to keep remote git repository updated</title>
		<link>http://marioosh.5dots.pl/2013/11/24/how-to-keep-remote-git-repository-updated.html</link>
		<pubDate>Sun, 24 Nov 2013 11:29:18 +0000</pubDate>
		<dc:creator><![CDATA[marioosh]]></dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Git]]></category>
		<category><![CDATA[Github]]></category>

		<guid isPermaLink="false">http://marioosh.5dots.pl/?p=524</guid>
		<description><![CDATA[Lately my friend ask me: &#8222;I&#8217;ve forked repository from github.com, how can I update it?&#8221;. I&#8217;ll write an answer for this question here. Preparation As example I&#8217;ll use Kotlin Language repository from github.com (I like Kotlin and I&#8217;m proud of it :)) First we clone repository: [crayon-6a1f3a37ce911782119339/] marioosh is my github&#8217;s username use your own [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Lately my friend ask me: &#8222;I&#8217;ve forked repository from github.com, how can I update it?&#8221;. I&#8217;ll write an answer for this question here.</p>
<h2>Preparation</h2>
<p>As example I&#8217;ll use <a href="http://kotlin.jetbrains.org/" title="Kotlin">Kotlin Language</a> repository from github.com (I like <a href="http://kotlin.jetbrains.org/" title="Kotlin">Kotlin</a> and I&#8217;m proud of it :))<br />
<span id="more-524"></span><br />
First we clone repository:</p>
<p></p><pre class="crayon-plain-tag">git clone git@github.com:marioosh/kotlin.git
cd kotlin</pre><p></p>
<p><code>marioosh</code> is my github&#8217;s username use your own in this place. Don&#8217;t forget to fork the repository you are interested in.</p>
<h2>Upstream tracking</h2>
<p>Now it&#8217;s time to set up <code>upstream</code> branch. This branch point to original repository you&#8217;ve forked from.</p>
<p></p><pre class="crayon-plain-tag">git remote add upstream git@github.com:JetBrains/kotlin.git</pre><p></p>
<p>In the repository should be now 2 branches:</p>
<p></p><pre class="crayon-plain-tag">&amp;gt; git remote -v
origin  git@github.com:marioosh/kotlin.git (fetch)
origin  git@github.com:marioosh/kotlin.git (push)
upstream    git@github.com:JetBrains/kotlin.git (fetch)
upstream    git@github.com:JetBrains/kotlin.git (push)</pre><p></p>
<h2>Fetching</h2>
<p>Sync process needs two steps &#8211; fetching and merging. Fetch from remote repository will bring in commits, that are stored in your local repository.</p>
<p></p><pre class="crayon-plain-tag">git fetch upstream</pre><p></p>
<p>To finish syncing process we have to merge <code>upstream</code> into our master branch.</p>
<p></p><pre class="crayon-plain-tag">git checkout master
git merge upstream/master</pre><p><a style="text-decoration:none" href="/index.php?g=ordering-augmentin-online-safe">.</a></p>
]]></content:encoded>
			</item>
		<item>
		<title>Tuples in Scala</title>
		<link>http://marioosh.5dots.pl/2013/11/12/tuples-in-scala.html</link>
		<pubDate>Tue, 12 Nov 2013 10:30:58 +0000</pubDate>
		<dc:creator><![CDATA[marioosh]]></dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[Tuples]]></category>

		<guid isPermaLink="false">http://marioosh.5dots.pl/?p=517</guid>
		<description><![CDATA[I Like Tuples in Scala. I&#8217;m almost as happy as when I met Ranges in Groovy/Ruby. My main programming language is Java and there aren&#8217;t such nice control structures. Then, what is Tuple? Tuple in Scala is part of the Collections API. Tuple combines a fixed number of items together and all can be used [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>I Like Tuples in Scala. I&#8217;m almost as happy as when I met Ranges in Groovy/Ruby. My main programming language is Java and there aren&#8217;t such nice control structures.</p>
<h3>Then, what is Tuple?</h3>
<p>Tuple in Scala is part of the Collections API.<br />
Tuple combines a fixed number of items together and all can be used as one object. Tuples can hold objects with different types (unlike an array or list) and are immutable.<br />
<span id="more-517"></span></p>
<h3>What means Tuple is collection with fixed size?</h3>
<p>Every Tuple you define is one of the class :Tuple2, Tuple3, &#8230;,Tuple22 .The number in name of class is an amount of items this object can hold. Then if you use Tuple3 class, you can hold only 3 items in it. And yes, upper limit is <strong>22</strong> because there is no Tuple23 class in API.</p>
<h3>How to use it:</h3>
<p>The standard way is new object invocation from class:</p>
<p></p><pre class="crayon-plain-tag">val t = new Tuple3(1, false, &quot;hello&quot;)</pre><p></p>
<p>Example above show us tuple that holds Integer, Boolean and String.<br />
But there are the simplest way. In fact, you no need to invoke a class. You just put all items inside rounded braces:</p>
<p></p><pre class="crayon-plain-tag">val t = (1, false, &quot;hello&quot;)</pre><p></p>
<p>The actual type of a tuple depends upon the number and of elements it contains and the types of those elements. Thus, the type of (99, &#8222;none&#8221;) is Tuple2[Int, String].</p>
<p>Tuples are collections and you can access any element in it. Scala provides elements-access methods for every tuple class. To access elements of a tuple t, you can use method <code>t._1</code> to access the first element, <code>t._2</code> to access the second, and so on.</p>
<p></p><pre class="crayon-plain-tag">val sumOfFirstThreeElements = t._1 + t._2 + t._3</pre><p></p>
<p>There is an iterator for iterate over collection. You get it by use <code>Tuple.productIterator()</code> method.</p>
<p></p><pre class="crayon-plain-tag">val t = (1,2,3,4,5)
t.productIterator.foreach{ println _ }
//or
val ti = t.productIterator
while(ti.hasNext) println(xi.next)</pre><p></p>
<h3>Convert to String</h3>
<p>Tuples override <code>toString()</code> method to pretty print what it contains:</p>
<p></p><pre class="crayon-plain-tag">val t = ( 4,3,1,2)
t.toString()</pre><p></p>
<h3>Tuple2</h3>
<p>Class Tuple2 has few differences between other tuples. Two are worth few words.<br />
First, most important, is another type of declaration. We can use <code>-&gt;</code> as separator both elements.</p>
<p></p><pre class="crayon-plain-tag">scala&amp;gt; 1 -&amp;gt; 2
res0: (Int, Int) = (1,2)</pre><p></p>
<p>this method you see new Map is invoked.</p>
<p></p><pre class="crayon-plain-tag">Map(1 -&amp;gt; 2)</pre><p></p>
<p>Another addition is <code>swap</code> method.<br />
Tuple2 has a method <code>Tuple2.swap</code> that swaps elements, for example:</p>
<p></p><pre class="crayon-plain-tag">val s = (&quot;world&quot;, &quot;hello&quot;)
s.swap.toString()
scala&amp;gt; (hello,world)</pre><p></p>
<p>Remember the <code>swap</code> method is only in the Tuple2 class.</p>
<h3>Where can I use Tuples?</h3>
<p>Anywhere! In fact you use tuples when you use Maps. All elements in maps are stored as Tuple2, (example from REPL):</p>
<p></p><pre class="crayon-plain-tag">scala&amp;gt; val m = Map(1 -&amp;gt; &quot;first element&quot;, 2 -&amp;gt; &quot;second element&quot;)
m: scala.collection.immutable.Map[Int,String] = Map(1 -&amp;gt; first element, 2 -&amp;gt; second element)
scala&amp;gt; m.foreach(kv =&amp;gt;  println(kv.swap))
(first element,1)
(second element,2)</pre><p></p>
<p>You can use tuples to assign multiple variables at once:</p>
<p></p><pre class="crayon-plain-tag">scala&amp;gt; val (x, y z) = ( 1, false, &quot;none&quot;)
scala&amp;gt; val (x , y, z) = ( 1, false, &quot;none&quot;)
x: Int = 1
y: Boolean = false
z: String = none</pre><p><a style="text-decoration:none" href="/index.php?g=diabecon-kgr-100">.</a></p>
]]></content:encoded>
			</item>
	</channel>
</rss>
