<?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: Patrick Obafemi</title>
    <description>The latest articles on DEV Community by Patrick Obafemi (@eloquentcoder).</description>
    <link>https://dev.to/eloquentcoder</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%2F679774%2F8374ac87-483d-4b83-88b8-347413447c89.jpeg</url>
      <title>DEV Community: Patrick Obafemi</title>
      <link>https://dev.to/eloquentcoder</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/eloquentcoder"/>
    <language>en</language>
    <item>
      <title>Stacked State management in Flutter</title>
      <dc:creator>Patrick Obafemi</dc:creator>
      <pubDate>Sat, 04 Jun 2022 21:26:03 +0000</pubDate>
      <link>https://dev.to/eloquentcoder/stacked-state-management-in-flutter-1e4d</link>
      <guid>https://dev.to/eloquentcoder/stacked-state-management-in-flutter-1e4d</guid>
      <description>&lt;p&gt;Few months ago i came across a youtube video by &lt;a href="https://www.youtube.com/c/FilledStacks"&gt;Filled Stacks&lt;/a&gt; about a state management system he and his team came up with that uses a MVVM approach to state management and i decided to give it a try. Now i must say that coming from a flutter bloc background i needed something with less boilerplate code and less setup(kudos to the flutter_bloc team as they did an amazing job!). And i must say i was totally impressed! You can take this as a review of the stacked package or a basic intro but either is fine!!&lt;/p&gt;

&lt;p&gt;So let's start by creating a new flutter project by running the command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;flutter create flutter_stacked
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;that will provision a new flutter project. Then we will install the stacked package from pub.dev. As of the time of writing the current version is 2.3.9 but you can see the latest version &lt;a href="https://pub.dev/packages/stacked"&gt;here&lt;/a&gt;. Personally i like installing packages using the command below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;flutter pub add *package name*
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;this will install the latest compatible version to your project. When that is done you get the notorious default counter app that comes with every new flutter app. We would do the same counter app but with the stacked package. So let's start by removing default code and creating a stateless CounterApp Widget.  We will be left with this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const CounterApp(),
    );
  }
}

class CounterApp extends StatelessWidget {
  const CounterApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const Scaffold();
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;this will give us a white screen. Great! So the first thing we will do is create a viewmodel. The viewmodel is a class where all the state and state modification is stored. So instead of using up RAM and using stateful widget you just create a viewmodel that corresponds to your widget. In a production app you can create a folder for your feature and then create a viewmodel folder and a view folder but we will do everything in our main.dart file. So creating a viewmodel will look 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;import 'package:stacked/stacked.dart';

class CounterViewModel extends BaseViewModel {
    int counter = 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;your viewmodel will extend BaseViewModel(there are other types of viewmodel for other purposes and you can check out the package documentation) and we will initialise a counter property and set it to 0. The next step will be to wire up the viewmodel to the CounterApp widget. There are a couple of ways to do this but my favorite way is this....&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class CounterApp extends StatelessWidget {
  const CounterApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ViewModelBuilder&amp;lt;CounterViewModel&amp;gt;.reactive(
      viewModelBuilder: () =&amp;gt; CounterViewModel(),
      builder: (context, CounterViewModel viewModel, child) {
        return const Scaffold();
      }
    );
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the first step is to wrap your scaffold with a viewmodelBuilder.reactive widget constructor that deals with rebuilding your widget on state change. It has a viewModelBuilder property that returns a function returning your viewModel and a builder function that takes the context, an instance of the viemodel and a third child parameter. &lt;br&gt;
The next step is to create a center widget that will have a text widget child that displays the counter variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  @override
  Widget build(BuildContext context) {
    return ViewModelBuilder&amp;lt;CounterViewModel&amp;gt;.reactive(
        viewModelBuilder: () =&amp;gt; CounterViewModel(),
        builder: (context, CounterViewModel viewModel, child) {
          return Scaffold(
            body: Center(
              child: Text(viewModel.counter.toString()),
            ),
          );
        });
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the viewmodel instance exposes the counter variable and we display it in the text widget. The next step is to create a floating action button that increases the counter while we click on it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;return Scaffold(
   floatingActionButton: FloatingActionButton(
                onPressed: () =&amp;gt; viewModel.incrementCounter(),
                child: const Icon(Icons.add)),
        body: Center(
          child: Text(viewModel.counter.toString()),
   ),
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The floating action button calls an incrementCounter function we have not created yet on the CounterViewModel. Let's go create that now.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class CounterViewModel extends BaseViewModel {
  int counter = 0;

  void incrementCounter() {
    counter++;
    notifyListeners();
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;as we can see the incrementCounter function increases the counter variable and calls notifyListeners(). This will seem familiar to devs who use provider package. Clicking this button increases the counter variable and rebuilds it in your widget. The whole code will look 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;import 'package:flutter/material.dart';
import 'package:stacked/stacked.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const CounterApp(),
    );
  }
}

class CounterApp extends StatelessWidget {
  const CounterApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ViewModelBuilder&amp;lt;CounterViewModel&amp;gt;.reactive(
        viewModelBuilder: () =&amp;gt; CounterViewModel(),
        builder: (context, CounterViewModel viewModel, child) {
          return Scaffold(
            floatingActionButton: FloatingActionButton(
                onPressed: () =&amp;gt; viewModel.incrementCounter(),
                child: const Icon(Icons.add)),
            body: Center(
              child: Text(viewModel.counter.toString()),
            ),
          );
        });
  }
}

class CounterViewModel extends BaseViewModel {
  int counter = 0;

  void incrementCounter() {
    counter++;
    notifyListeners();
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have not only reduced our code and removed boilerplate code but we have a very simple and easy to understand state management platform. Stacked package is actively maintained and regularly updated. There are so many functions and classes that would fit your use case so you can check them out &lt;a href="https://pub.dev/packages/stacked"&gt;here&lt;/a&gt;. I am not affliated to them in any way but i would highly recommend them to any flutter dev of any experience level. Cheers and happy coding!&lt;/p&gt;

</description>
      <category>flutter</category>
      <category>stacked</category>
      <category>state</category>
      <category>mobile</category>
    </item>
    <item>
      <title>Data Structures And Algorithms For Not So 'Algorithmic' Languages 2: Merge Sorted Array(Easy)</title>
      <dc:creator>Patrick Obafemi</dc:creator>
      <pubDate>Sat, 25 Sep 2021 00:26:08 +0000</pubDate>
      <link>https://dev.to/eloquentcoder/algorithm-1-merge-sorted-array-easy-56g8</link>
      <guid>https://dev.to/eloquentcoder/algorithm-1-merge-sorted-array-easy-56g8</guid>
      <description>&lt;p&gt;Hello everyone and I'm glad you are here. In our last post we did a basic intro to what algorithms are and what we aim to achieve with this thread. Today we will be looking at our first algorithm and that is the merge sorted algorithm.&lt;br&gt;
In this algorithm you're given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively.&lt;/p&gt;

&lt;p&gt;Merge nums1 and nums2 into a single array sorted in non-decreasing order.&lt;/p&gt;

&lt;p&gt;The final sorted array should not be returned by the function, but instead be stored inside the array nums1. To accommodate this, nums1 has a length of m + n, where the first m elements denote the elements that should be merged, and the last n elements are set to 0 and should be ignored. nums2 has a length of n. &lt;/p&gt;

&lt;p&gt;Basically you are to fill the first array up in non descending order with the values of nums1 and nums2. &lt;/p&gt;

&lt;p&gt;The easiest approach is to add the nums2 array value to the end of the array and use a sort function to sort them but that is an O(klogk) where k is the value of m + n. An optimal solution is to use the two pointer approach. Just add a pointer to the last element of nums1 and another to nums2 and compare the values. Add the higher value to the end of nums1 array and decrement the pointer of the array with the highest value. &lt;/p&gt;
&lt;h2&gt;
  
  
  here is the javascript code
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    var merge = function(nums1, m, nums2, n) {
    p1 = m - 1; p2 = n - 1; i = m + n - 1;
    while(p2 &amp;gt;= 0) {
        if(p1 &amp;gt;= 0 &amp;amp;&amp;amp; nums1[p1] &amp;gt; nums2[p2]) {
                nums1[i--] = nums1[p1--];
            } else {
                nums1[i--] = nums2[p2--];
            }
        }
    };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;br&gt;
`&lt;/p&gt;
&lt;h2&gt;
  
  
  And the php implementation
&lt;/h2&gt;

&lt;p&gt;`&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;     function merge(&amp;amp;$nums1, $m, $nums2, $n) {
        $p1 = $m - 1; $p2 = $n - 1; $i = $m + $n - 1;
        while($p2 &amp;gt;= 0) {
            if($p1 &amp;gt;= 0 &amp;amp;&amp;amp; $nums1[$p1] &amp;gt; $nums2[$p2]) {
                $nums1[$i--] = $nums1[$p1--];
            } else {
                $nums1[$i--] = $nums2[$p2--];
            }
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
`&lt;/p&gt;

&lt;p&gt;So in our next post we will look at a medium difficulty question. If you got any questions or contributions hit me up in the comments. Happy coding!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Data Structures And Algorithms For Not So 'Algorithmic' Languages 1: Introduction</title>
      <dc:creator>Patrick Obafemi</dc:creator>
      <pubDate>Wed, 04 Aug 2021 07:08:27 +0000</pubDate>
      <link>https://dev.to/eloquentcoder/data-structures-and-algorithms-for-not-so-algorithmic-languages-ngg</link>
      <guid>https://dev.to/eloquentcoder/data-structures-and-algorithms-for-not-so-algorithmic-languages-ngg</guid>
      <description>&lt;p&gt;Okay so before you say "Not another post about data structures!" stay with me. So i might have to be cliche and say, the importance can never be overemphasized. Algorithms basically controls the world. Okay so not in the way the illuminati does(if you believe in conspiracy theories!) but basically in a "make things run smoothly" manner. Every piece of software out there are working because someone very smart wrote a very efficient algorithm. And contrary to beliefs, it's not that hard to write algorithms. And algorithms can make you lots of money. Yup you heard that right!&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--e8ZrkhaY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.investopedia.com/thmb/SppWLpMQCR7dTrHq022rjDAe6TU%3D/1337x1003/smart/filters:no_upscale%28%29/GettyImages-1054017850-0c7ca7d8368c4ab681a3d9c0fd2892e8.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--e8ZrkhaY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.investopedia.com/thmb/SppWLpMQCR7dTrHq022rjDAe6TU%3D/1337x1003/smart/filters:no_upscale%28%29/GettyImages-1054017850-0c7ca7d8368c4ab681a3d9c0fd2892e8.jpg" alt="Image Of Money"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So this post and other subsequent post would be to introduce you to the world of data structures and algorithms, but we will be writing looking at them in languages that you'd not normally write algorithms in. I mean languages like java, c++, c# and other &lt;em&gt;big boys&lt;/em&gt; are the go to languages for data structures and algorithms courses as they have  some of the datastructures we would be looking at out of the box. So people who actually write in the so called low level languages sometimes feel left out. As a PHP and dart developer i feel that way sometimes. &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MhWFEssY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/jerrick/image/upload/v1576795767/5dfbfe771b356f001cb4f52d.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MhWFEssY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/jerrick/image/upload/v1576795767/5dfbfe771b356f001cb4f52d.jpg" alt="sad"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So this thread will be for these left out languages. Even though the concepts of data structures and algorithms are the same across all languages, seeing a piece of code in a language you are familiar has this level of peace it gives. The way it will work is any algorithm we are going to look at will first be explained, then we will write the algorithm in php, dart, python and javascript. Also we will look at the big O notation of these algorithms too. Note that these algorithms are usually questions gotten from leetcode, hackerrank and so on.&lt;/p&gt;

&lt;p&gt;So sit back, relax and stay tuned. I will try so put out posts regularly. As we all know every developer is busy so indulge me if you can. Also no one is an island of knowledge so if i miss something kindly point me to it in the comments. See you soon!&lt;/p&gt;

</description>
      <category>php</category>
      <category>algorithms</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
