<?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: ahmedmenaem</title>
    <description>The latest articles on DEV Community by ahmedmenaem (@ahmedmenaem).</description>
    <link>https://dev.to/ahmedmenaem</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%2F125429%2F16a06cc7-3cae-4a4d-808a-77abfadab514.jpeg</url>
      <title>DEV Community: ahmedmenaem</title>
      <link>https://dev.to/ahmedmenaem</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ahmedmenaem"/>
    <language>en</language>
    <item>
      <title>The cost of amortized time complexity</title>
      <dc:creator>ahmedmenaem</dc:creator>
      <pubDate>Sat, 09 May 2020 23:48:29 +0000</pubDate>
      <link>https://dev.to/ahmedmenaem/the-cost-of-amortized-time-complexity-22h7</link>
      <guid>https://dev.to/ahmedmenaem/the-cost-of-amortized-time-complexity-22h7</guid>
      <description>&lt;h2&gt;
  
  
  Amortized Time Complexity, worst case happens every once in a while.
&lt;/h2&gt;

&lt;p&gt;I'm so tired of searching for good resources that discussing amortized time complexity, so &lt;strong&gt;what is the meaning of amortized time?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;first thing let's define the meaning for the array, and why we need it?&lt;/p&gt;

&lt;h3&gt;
  
  
  Array
&lt;/h3&gt;

&lt;p&gt;In computer science, an array data structure or simply an &lt;strong&gt;array&lt;/strong&gt; is a data structure that consisting of a collection of elements values or variables.&lt;/p&gt;

&lt;p&gt;let's simplify it, an array is a data structure that can contain multiple values, we can think of it as a list of multiple things.&lt;/p&gt;

&lt;p&gt;an array is a contiguous block of memory, let's imagine that we have an array of size 5, so it will reserve 5 blocks &lt;strong&gt;(block after block)&lt;/strong&gt; in the memory, so memory will always have a &lt;strong&gt;fixed size&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ysgMXzMv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://beginnersbook.com/wp-content/uploads/2018/10/array.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ysgMXzMv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://beginnersbook.com/wp-content/uploads/2018/10/array.jpg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  How programming languages solve the fixed-size problem to give us the ability to create a dynamical size array.
&lt;/h4&gt;

&lt;p&gt;Many programming languages solve this problem by creating an &lt;strong&gt;ArrayList&lt;/strong&gt; or &lt;strong&gt;Dynamically resizing array&lt;/strong&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  What is array list?
&lt;/h4&gt;

&lt;p&gt;is a data structure that allows us to have the benefits of an array while offering flexibility in size, you won't run out of space since in the &lt;strong&gt;Array List&lt;/strong&gt; will grow as you insert elements.&lt;/p&gt;

&lt;p&gt;The Array List is implemented with an array as the hurt of its logic when the array hits capacity, the Array List will create a new array with double the capacity and copy all the elements over the new area.&lt;/p&gt;

&lt;h4&gt;
  
  
  Whaaaaaat, what did you said ??
&lt;/h4&gt;

&lt;p&gt;Yeh, it is doing 2 operations:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;create a new array with double size of the old one.&lt;/li&gt;
&lt;li&gt;copy all elements over to the new array.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;as we know that when we insert new value at the end of the array, it is required a single operation with constant time complexity O(1)&lt;/strong&gt;, Please check this link, if you don't know, how to describe your runtime &lt;a href="https://en.wikipedia.org/wiki/Big_O_notation"&gt;Big O noation&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  So, What is the runtime (complexity) of the insertion?
&lt;/h3&gt;

&lt;p&gt;let's explain how it works?, the array could be full, if the array contains &lt;strong&gt;N elements&lt;/strong&gt; then inserting new element will take O(n) time because we have to create a new array of size 2*N and then copy N elements over the new array.&lt;/p&gt;

&lt;h4&gt;
  
  
  we know that it doesn't happen very often, the vast majority of the time insertion will be O(1).
&lt;/h4&gt;

&lt;h3&gt;
  
  
  So, what is amortized time does?
&lt;/h3&gt;

&lt;p&gt;Amortized time allows us to describe these cases, cases that happen every once in a while, and won't happen again for a while.&lt;/p&gt;

&lt;p&gt;In case that we discussed, the amortized time is:&lt;/p&gt;

&lt;p&gt;when we insert a new element, the first thing we are going to double the array capacity the double-takes respectively 1, 2, 4, 8, 16, 32, 64, X, so what we mean by the sum of this equation? this is roughly 2X.&lt;/p&gt;

&lt;p&gt;Therefore, X insertion takes O(2X). the amortized time for each insertion is O(1), so we are not saying it O(1) time complexity, it is amortized O(1) time complexity.&lt;/p&gt;

&lt;h4&gt;
  
  
  Summary
&lt;/h4&gt;

&lt;p&gt;Amortized time describes cases that happen every once in a while, and won't happen again for a while.&lt;/p&gt;

&lt;h4&gt;
  
  
  resources
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Amortized_analysis"&gt;Amortized time&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.crackingthecodinginterview.com/"&gt;Cracking the coding interview&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>array</category>
      <category>datastructures</category>
      <category>amotrizedtime</category>
      <category>bigo</category>
    </item>
    <item>
      <title>Reactive programming for lazy programmers.</title>
      <dc:creator>ahmedmenaem</dc:creator>
      <pubDate>Sun, 03 May 2020 11:13:29 +0000</pubDate>
      <link>https://dev.to/ahmedmenaem/reactive-programming-for-lazy-programmers-lon</link>
      <guid>https://dev.to/ahmedmenaem/reactive-programming-for-lazy-programmers-lon</guid>
      <description>&lt;h1&gt;
  
  
  &lt;strong&gt;Introduction to Reactive programming for lazy programmers.&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;I know you are tired and exhausted from searching for a good introduction about reactive programming, you listened for a lot of people but finally, you find this series.&lt;/p&gt;

&lt;p&gt;In this series, I'm going to give you the bedrock of reactive programming,&lt;br&gt;
let's dive together and learn what we need.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Reactive Programming?
&lt;/h2&gt;

&lt;p&gt;There are plenty of bad explanations and definitions about reactive programming, so let's cut it in a little definition.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reactive Programming
&lt;/h3&gt;

&lt;p&gt;is programming with asynchronous data streams.&lt;/p&gt;

&lt;h4&gt;
  
  
  Oh Whaaaaaat ???
&lt;/h4&gt;

&lt;p&gt;come down, this isn't anything new, event buses or typical click events are really an asynchronous event stream which you can observe and do some side effects, so &lt;strong&gt;what is the reactive programming main idea?&lt;/strong&gt; reactive programming gives you the ability to create data streams of anything.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Streams:&lt;/strong&gt; are cheap and ubiquitous, anything can be a stream, variables, user inputs, properties, caches, data structure, etc..&lt;/p&gt;

&lt;h3&gt;
  
  
  Stream
&lt;/h3&gt;

&lt;p&gt;a stream is a sequence of ongoing events ordered in time, &lt;br&gt;
it can emit 3 different things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;value (of some type)&lt;/li&gt;
&lt;li&gt;error (something wrong happened)&lt;/li&gt;
&lt;li&gt;completed signal (when the stream is done or completed)
&lt;img src="https://camo.githubusercontent.com/36c0a9ffd8ed22236bd6237d44a1d3eecbaec336/687474703a2f2f692e696d6775722e636f6d2f634c344d4f73532e706e67" alt=""&gt;
we captured these emitted events only asynchronously, by defining a function that will execute when a &lt;strong&gt;value&lt;/strong&gt; is emitted, another function when an &lt;strong&gt;error&lt;/strong&gt; is emitted and another function when &lt;strong&gt;completed&lt;/strong&gt; is emitted, sometimes we can omit the last 2 functions and we can just focus on defining the function for values.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Subscribe
&lt;/h3&gt;

&lt;p&gt;The listening to the stream is called subscribing. by subscribing to an &lt;br&gt;
observable you are going listen to any new change&lt;/p&gt;

&lt;h3&gt;
  
  
  Observer
&lt;/h3&gt;

&lt;p&gt;The functions that we are defining to deal with the ongoing events are called observers, it contains 3 functions (&lt;strong&gt;next&lt;/strong&gt;, &lt;strong&gt;error&lt;/strong&gt;, &lt;strong&gt;complete&lt;/strong&gt;).&lt;br&gt;
It is an object usually given to the &lt;strong&gt;observable.subscribe(observer)&lt;/strong&gt;, Observable will call the Observer's &lt;strong&gt;next(value)&lt;/strong&gt; method to provide notifications, a well-behaved Observable will call an Observer's &lt;strong&gt;complete()&lt;/strong&gt; method exactly once or the Observer's &lt;strong&gt;error(err)&lt;/strong&gt; method exactly once, as the last notification delivered.&lt;/p&gt;

&lt;h3&gt;
  
  
  Subject
&lt;/h3&gt;

&lt;p&gt;Observable or subject is what is observed by observers.&lt;br&gt;
we can imagine it as a data store that will send a notification whenever change happened to its value.&lt;/p&gt;

&lt;h4&gt;
  
  
  Oh that's the Observer Design Pattern, &lt;a href="https://en.wikipedia.org/wiki/Observer_pattern"&gt;so please check it&lt;/a&gt;
&lt;/h4&gt;

&lt;h3&gt;
  
  
  What is the difference between Promise and Observable?
&lt;/h3&gt;

&lt;p&gt;There is no big difference, both are promising us with a value in the future when some event happens, but the main difference between them that Promise will emit a &lt;strong&gt;single value&lt;/strong&gt;, on the other side Observable returned &lt;strong&gt;many values&lt;/strong&gt; over time. &lt;/p&gt;

&lt;h3&gt;
  
  
  Why should I consider adopting Reactive Programming?
&lt;/h3&gt;

&lt;p&gt;Reactive programming raises the level of abstraction of our code, so we can focus on the interdependence of events that define the business logic, rather than having to constantly fiddle with a large amount of implementation details. &lt;strong&gt;Code in Reactive Programming will likely be more concise&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Benefits of using Reactive Programming
&lt;/h3&gt;

&lt;p&gt;The benefits are more evident in modern web apps and mobile apps that are highly interactive with a multitude of UI related to data events, for example, "likes" on Facebook can be reflected in realtime to other connected users.&lt;/p&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;Reactive programming is really suitable for real-time web apps and mobile applications, it is really helpful when we are dealing with multiple components dealing with the same data source and interested in data change.&lt;/p&gt;

&lt;p&gt;That's a simple intro about reactive programming and in the next article, we are going to talk about how to implement it using RXJS the reactive extension for javascript. &lt;/p&gt;

&lt;h3&gt;
  
  
  Links
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://egghead.io/courses/introduction-to-reactive-programming"&gt;introduction-to-reactive-programming&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://gist.github.com/staltz/868e7e9bc2a7b8c1f754"&gt;The introduction to Reactive Programming you've been missing&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Observer_pattern"&gt;Observer pattern&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Please check this links and your feedback is really matter
&lt;/h4&gt;

</description>
      <category>reactiveprogramming</category>
      <category>rxjs</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
