<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Bruno Zani</title>
    <description>The latest articles on DEV Community by Bruno Zani (@bzani).</description>
    <link>https://dev.to/bzani</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F606547%2F848ae38b-b441-4cee-a290-d2225ffa85c5.jpeg</url>
      <title>DEV Community: Bruno Zani</title>
      <link>https://dev.to/bzani</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bzani"/>
    <language>en</language>
    <item>
      <title>WSL2, Docker, GUI - Manual Installation</title>
      <dc:creator>Bruno Zani</dc:creator>
      <pubDate>Sun, 11 Apr 2021 20:45:11 +0000</pubDate>
      <link>https://dev.to/bzani/wsl2-docker-gui-3o0j</link>
      <guid>https://dev.to/bzani/wsl2-docker-gui-3o0j</guid>
      <description>&lt;h1&gt;
  
  
  WSL and WSL2
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Make sure that&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You have Windows 10 version 1903 or higher, with Build 18362 or higher. If not, update it.

&lt;ul&gt;
&lt;li&gt;Check it by using &lt;code&gt;Win+R&lt;/code&gt;, command &lt;code&gt;winver&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Virtualization is enabled inside of your computer's BIOS&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Enable WSL on Windows 10&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Run the commands below, using PowerShell on Admin mode:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Open your PowerShell once again and try typing &lt;code&gt;wsl&lt;/code&gt;, and if doesn't work, restart your machine&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Upgrade to WSL2&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi"&gt;Download&lt;/a&gt; and install the WSL2 kernel to update&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Set the WSL default version to 2, via Powershell:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;wsl --set-default-version 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;(in case of error, ignore it this time)&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Linux Distro
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Install the Ubuntu distro&lt;/strong&gt; (or any other you like)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Open your Microsoft Store, search for the Ubuntu App, select the &lt;code&gt;Ubuntu&lt;/code&gt; app (which is the latest) and install it&lt;/li&gt;
&lt;li&gt;After installed, configure your root user and password&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Upgrade WSL version for Ubuntu&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;List your distro using &lt;code&gt;wsl --list&lt;/code&gt;, via Powershell&lt;/li&gt;
&lt;li&gt;Set your distro to WSL version 2
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;wsl --set-version &amp;lt;distro_name&amp;gt; 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Tips&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Access your WSL "machine" through the &lt;code&gt;wsl&lt;/code&gt; command (via Powershell or cmd), which stands for:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;C:\Windows\System32\wsl.exe
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Access your WSL "space" on Windows through: (&lt;code&gt;Win+R&lt;/code&gt; or explorer):
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;\\wsl$
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Find your Windows "space" on Linux:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/mnt/c/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;You can also run Windows programs from WSL (try &lt;code&gt;explorer.exe&lt;/code&gt;, or &lt;code&gt;code .&lt;/code&gt; for VS Code, for example), however it doesn't mean all of them will work alright&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Have in mind that WSL performance is strictly related to Linux usage, i.e., using WSL to manage and work with your files in &lt;code&gt;/mnt/c&lt;/code&gt; won't help&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Find your WSL "disk" on Windows: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;in the case of Ubuntu, we have something like this:
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;C:\Users\&amp;lt;User&amp;gt;\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu...\LocalState
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Backup your WSL state:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;wsl --export &amp;lt;distro&amp;gt; &amp;lt;output_file&amp;gt;                        # export
wsl --import &amp;lt;distro&amp;gt; &amp;lt;installation_location&amp;gt; &amp;lt;input_file&amp;gt; # import
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;or even, create a backup of the WSL "disk" file (previous item)&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;WSL2 uses almost all your machines resources, by default: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Disk and CPU &lt;/li&gt;
&lt;li&gt;80% of your available RAM &lt;/li&gt;
&lt;li&gt;25% of your available memory for Swap &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;You can set up limits, by creating a config file (example): &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;C:\Users\&amp;lt;User&amp;gt;\.wslconfig &amp;gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[wsl2] 
options=metadata,umask=22,fmask=11 
memory=8GB 
processors=4 
swap=2GB 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Your custom configs are applied after a WSL restart:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;wsl --shutdown
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Windows Terminal
&lt;/h1&gt;

&lt;p&gt;Windows Terminal is a new feature from Microsoft, improving your experience with terminals in Windows.&lt;/p&gt;

&lt;p&gt;Install via Microsoft Store, by searching for &lt;code&gt;Windows Terminal&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;With Windows Terminal you can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;have multiple terminals opened in separate tabs (instances)&lt;/li&gt;
&lt;li&gt;different terminals running along (Ubuntu, git bash, Powershell, etc)&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://docs.microsoft.com/pt-br/windows/terminal/customize-settings/startup"&gt;customize it&lt;/a&gt; by applying themes, configuring colors and shortcuts, etc

&lt;ul&gt;
&lt;li&gt;change file &lt;code&gt;settings.json&lt;/code&gt; by hitting &lt;code&gt;Ctrl+,&lt;/code&gt; on Windows Terminal&lt;/li&gt;
&lt;li&gt;OR, open file from default dir:
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;C:\Users\&amp;lt;User&amp;gt;\AppData\Local\Packages\Microsoft.WindowsTerminal...\LocalState\settings.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;you can change e.g. your Linux starting directory and default shell, when opening Windows Terminal:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"defaultProfile": "{xxxxx-xxxx-xxxx-xxxx}",
...
{ 
    "guid": "{xxxxx-xxxx-xxxx-xxxx}",
    "name": "Ubuntu-18.04", 
    "startingDirectory" : "//wsl$/Ubuntu/home/&amp;lt;ubuntu_user&amp;gt;", 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Docker
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;3 ways to use Docker on Windows&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Docker Toolbox &lt;em&gt;- bad&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Uses Oracle VirtualBox &lt;/li&gt;
&lt;li&gt;Very bad performance &lt;/li&gt;
&lt;li&gt;Difficult setup and usage &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;Docker Desktop com Hyper-V &lt;em&gt;- better&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Uses Microsoft Hyper-V instead of VirtualBox &lt;/li&gt;
&lt;li&gt;Requires Windows 10 PRO &lt;/li&gt;
&lt;li&gt;Better performance but a heavy consumer of host resources &lt;/li&gt;
&lt;li&gt;Docker will drop support in the future, as announced &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;Docker Desktop com WSL2 &lt;em&gt;- &lt;strong&gt;recommended *&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Uses Microsoft Virtual Machine Platform &lt;/li&gt;
&lt;li&gt;Integrates with WSL2 and its distros &lt;/li&gt;
&lt;li&gt;Better performance and consumes less resources &lt;/li&gt;
&lt;li&gt;Usage as running on Linux natively&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;*&lt;strong&gt;Download and install Docker Desktop&lt;/strong&gt;&lt;br&gt;
    &lt;a href="https://hub.docker.com/editions/community/docker-ce-desktop-windows"&gt;https://hub.docker.com/editions/community/docker-ce-desktop-windows&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Enable Docker on WSL2 distro&lt;/strong&gt;&lt;br&gt;
    - Docker Desktop &amp;gt; Settings &amp;gt; Resources &amp;gt; WSL Integration &lt;br&gt;
        &amp;gt; Enable integration with additional distros &amp;gt; Check distro&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Play with docker on WSL&lt;/strong&gt;&lt;br&gt;
    - Open WSL and hit &lt;code&gt;docker ps&lt;/code&gt; or try out &lt;code&gt;docker run hello-world&lt;/code&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  WSL Linux GUI
&lt;/h1&gt;

&lt;p&gt;&lt;em&gt;Optional&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;A way to run Linux apps using WSL "builtin GUIs" is on the way, as one of the WSL improvements of Microsoft's roadmap.&lt;/p&gt;

&lt;p&gt;While it still unavailable, we can still install a GUI and use RDP (Remote desktop), to access the Linux VM.&lt;/p&gt;

&lt;p&gt;First of all, update and upgrade your Linux:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt update &amp;amp;&amp;amp; sudo apt -y upgrade
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Install (or overwrite) X-RDP:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt-get purge xrdp
sudo apt install -y xrdp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Install a GUI:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt install -y xfce4 # check 'gdm3' option
sudo apt install -y xfce4-goodies
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;XFCE is a lightweight GUI for testing purposes&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Configure access, create a backup, improve RDP session quality and define session:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo cp /etc/xrdp/xrdp.ini /etc/xrdp/xrdp.ini.bak
sudo sed -i 's/3389/3390/g' /etc/xrdp/xrdp.ini
sudo sed -i 's/max_bpp=32/#max_bpp=32\nmax_bpp=128/g' /etc/xrdp/xrdp.ini
sudo sed -i 's/xserverbpp=24/#xserverbpp=24\nxserverbpp=128/g' /etc/xrdp/xrdp.ini
echo xfce4-session &amp;gt; ~/.xsession
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Edit XRDP startup script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo nano /etc/xrdp/startwm.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Comment these last lines:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# test -x /etc/X11/Xsession &amp;amp;&amp;amp; exec /etc/X11/Xsession 
# exec /bin/sh /etc/X11/Xsession 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Add these lines:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# xfce
startxfce4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Start RDP session:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo /etc/init.d/xrdp start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Access it from your Windows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Run command &lt;code&gt;mstsc&lt;/code&gt; (&lt;code&gt;Win+R&lt;/code&gt;)
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;localhost:3390
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Dev Projects
&lt;/h1&gt;

&lt;p&gt;There are many softwares that already works integrated with WSL:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;on VS Code for example, you can find add-ons to work "remotely" with your projects in WSL, as you can see &lt;a href="https://code.visualstudio.com/docs/remote/wsl"&gt;here&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;IntelliJ also have its ways to deal with WSL integration and run along with your projects, as you can see &lt;a href="https://www.jetbrains.com/help/idea/how-to-use-wsl-development-environment-in-product.html#local_project"&gt;here&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;on the other hand, there may be softwares that are more complex to integrate with WSL (or even don't)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you have any further info, specially about dev workspaces with WSL, just drop me a line below and help everyone enjoy and experience the benefits of this tool.&lt;/p&gt;

&lt;p&gt;See ya.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Source&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://docs.microsoft.com/en-us/windows/wsl/install-win10#manual-installation-steps"&gt;https://docs.microsoft.com/en-us/windows/wsl/install-win10#manual-installation-steps&lt;/a&gt;&lt;br&gt;
&lt;a href="https://github.com/codeedu/wsl2-docker-quickstart"&gt;https://github.com/codeedu/wsl2-docker-quickstart&lt;/a&gt; (pt-br)&lt;br&gt;
&lt;a href="https://github.com/davidbombal/wsl2/blob/main/ubuntu_gui_youtube"&gt;https://github.com/davidbombal/wsl2/blob/main/ubuntu_gui_youtube&lt;/a&gt;&lt;/p&gt;

</description>
      <category>wsl2</category>
      <category>docker</category>
      <category>wslgui</category>
      <category>windows</category>
    </item>
    <item>
      <title>Java streams and Fibonacci</title>
      <dc:creator>Bruno Zani</dc:creator>
      <pubDate>Sat, 03 Apr 2021 21:20:12 +0000</pubDate>
      <link>https://dev.to/bzani/java-streams-and-fibonacci-2p9l</link>
      <guid>https://dev.to/bzani/java-streams-and-fibonacci-2p9l</guid>
      <description>&lt;p&gt;So, you still struggling trying to understand expressions like this in Java 8+:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Stream.iterate(new long[]{ 0L, 1L }, p-&amp;gt;new long[]{ p[1], p[0]+p[1] }).map(p -&amp;gt; p[0]).limit(10).map(p-&amp;gt;p+" ").forEach(System.out::print);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Do you have a clue about what this line is about to do, when you hit Run? Yes, you're right, you'll have the Fibonacci sequence of 10 elements printed in your console, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0 1 1 2 3 5 8 13 21 34
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's a tricky one, and if you didn't answer that correctly, I'll try something easier before digging into an explanation.&lt;/p&gt;

&lt;p&gt;So, your second chance is right here:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Arrays.asList(1, 2, 2, 3, 4, 4, 4, 5, 6, 8).stream()
    .distinct()
    .filter(x -&amp;gt; x % 2 == 0)
    .map(x -&amp;gt; x * 10)
    .skip(1)
    .limit(2)
    .peek(System.out::println)
    .reduce(0, Integer::sum)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Don't need no math right now, because what I wanted to show you is how this works step by step. &lt;/p&gt;

&lt;p&gt;So, first of all, what a &lt;em&gt;STREAM&lt;/em&gt; is?&lt;/p&gt;

&lt;p&gt;Must have in mind is that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Streams are &lt;strong&gt;not&lt;/strong&gt; a data structure&lt;/li&gt;
&lt;li&gt;Streams doesn't store or modify data&lt;/li&gt;
&lt;li&gt;It can be defined as a &lt;strong&gt;wrapper&lt;/strong&gt; around a data source&lt;/li&gt;
&lt;li&gt;Supports functional-style operations&lt;/li&gt;
&lt;li&gt;Fast and convenient bulk processing over a data source&lt;/li&gt;
&lt;li&gt;Iteration over elements are sequential and declarative&lt;/li&gt;
&lt;li&gt;Parallel-friendly, immutable and thread-safe&lt;/li&gt;
&lt;li&gt;Lazy evaluation&lt;/li&gt;
&lt;li&gt;Single use&lt;/li&gt;
&lt;li&gt;Don't confuse with Java I/O (ex. FileInputStream)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Besides that, we have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Intermediate operations: stream chained operations that return new streams (pipeline)&lt;/li&gt;
&lt;li&gt;Terminal operations: execute the stream and produces a non-stream object (result)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can create a stream by invoking the methods &lt;code&gt;stream()&lt;/code&gt; or &lt;code&gt;parallelStream()&lt;/code&gt;, or even use &lt;code&gt;Stream.of&lt;/code&gt;, &lt;code&gt;Stream.ofNullable&lt;/code&gt; or &lt;code&gt;Stream.iterate&lt;/code&gt; (like I did for Fibonacci).&lt;/p&gt;

&lt;p&gt;Let's try to get back a little bit and "debug" the easy example up there:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Arrays.asList(1, 2, 2, 3, 4, 4, 4, 5, 6, 8)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Creates a new Collection, so we can iterate through its elements via stream&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.stream()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Starts a new stream&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.distinct()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Retain distinct elements, returning: &lt;code&gt;[1, 2, 3, 4, 5, 6, 8]&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.filter(x -&amp;gt; x % 2 == 0)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Filter pair elements by applying mod 2, returning: &lt;code&gt;[2, 4, 6, 8]&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.map(x -&amp;gt; x * 10)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Multiplicates each filtered element by 10, returning: &lt;code&gt;[20, 40, 60, 80]&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.skip(1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Skips the first element of the sequence, returning: &lt;code&gt;[40, 60, 80]&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.limit(2)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Returns the first 2 elements of the sequence: &lt;code&gt;[40, 60]&lt;/code&gt;&lt;br&gt;
This is a "short-circuit" command, i.e., it'll interrupt the stream processing when the condition is match - in this case, when we reach the second element&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.peek(System.out::println)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Some kind of "debug" mode of stream, in this case it'll &lt;code&gt;println&lt;/code&gt; each remaining element of the stream operation: &lt;code&gt;40&lt;/code&gt; and &lt;code&gt;60&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.reduce(0, Integer::sum)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, we're gonna effectively consume the stream and get a Integer result from it: the sum of all elements, where &lt;code&gt;0&lt;/code&gt; is the starting value, and &lt;code&gt;Integer::sum&lt;/code&gt; invokes the &lt;code&gt;Integer.sum(int a, int b)&lt;/code&gt; function from the Integer class, that will be performed over each remaining element from the stream operation, that is, 40 and 60, then resulting &lt;strong&gt;100&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Now, what about the Fibonacci stuff up there? Right on, let's get back to it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Stream.iterate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By definition, we have: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Returns an infinite sequential ordered Stream produced by iterative application of a function f to an initial element seed, producing a Stream consisting of seed, f(seed), f(f(seed)), etc. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That said, all we have to do is to provide a seed (i.e., the starting point, or, the initial element) and then a function  to be applied to the previous element to produce a new element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;new long[]{ 0L, 1L } // as the 1st parameter
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For the Fibonacci "kickstart" we need two elements: &lt;code&gt;0&lt;/code&gt; and &lt;code&gt;1&lt;/code&gt;. Then, the sum of both will provide the next element and so on. That said, this array will represent a pair of the first values of the Fibonacci sequence, as the first element of the stream.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;p-&amp;gt;new long[]{ p[1], p[0]+p[1] }) // as the 2nd parameter
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, this object &lt;code&gt;p&lt;/code&gt; will create a new array containing two elements: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;p[1]&lt;/code&gt;: which is the previous array in the position 1;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;p[0] + p[1]&lt;/code&gt;: which is the previous array in the position 0 plus its position 1;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.map(p -&amp;gt; p[0])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The thing is, we have a stream of pair elements going on until now, however, our main goal is to print elements of this sequence. To get the "current" element of this operation, by "mapping" the current pair of values of this stream to a single element, in this case, the array in position &lt;code&gt;0&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.limit(10)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is important, as said before, this will &lt;em&gt;Return an infinite sequential ordered Stream&lt;/em&gt; by default. So, in order not to have this running forever, with your PC burning out, we define a "limit" to this operation, by processing the first 10 elements only.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.map(p-&amp;gt;p+" ")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is silly, as we want to just print the sequence, it would be nice if we have a separator for each element, so here it is.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.forEach(System.out::print)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;forEach&lt;/code&gt; is a terminal operation as well, and what it'll do is to iterate over the resulting collection of the Stream. So, here we're just printing the current element by using the well-known &lt;code&gt;System.out.print&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;If you're not familiar with expressions like this &lt;code&gt;System.out::print&lt;/code&gt; yet, better check out about method reference, which is a special type of lambda expression, very useful to reduce boilerplate and improve readability. Next chapter suggested to write about is on Java functional programming.&lt;/p&gt;

</description>
      <category>java</category>
      <category>fibonacci</category>
      <category>streams</category>
      <category>code</category>
    </item>
  </channel>
</rss>
