teleren/devto huggingface dataset:
Usando o filter e reduce na prática.
Tags: javascript
Published: 2020-07-06 01:16:20

Neste artigos vamos aplicar as funções filter e reduce do javascript para calcular o saldo da conta bancária.

Temos o seguinte array de objetos, que representam a quantidade de ganhos ( income ) e gastos ( outcome ).

```js
const transactions = [
  {
    title: "Salário",
    value: 3000,
    type: "income"
  },
  {
    title: "Freela",
    value: 1000,
    type: "income"
  },
  {
    title: "Boleto",
    value: 2000,
    type: "outcome"
  },
  {
   title: "conta",
   value: 500,
   type: "outcome"
   },
];

```
Nosso objetivo é escrever uma função que retorne o valor total de "outcome", "income" e a diferença entre eles.

Vamos utilizar a seguinte estratégia. Primeiro vamos usar o filter para separar o tipo de gasto e depois somamos o array de objetos com o reduce.

A função filter, cria uma novo array, com o resultado que passou na nossa implementação.

```js
const filteredIncome = transactions.filter( transaction => transaction.type === 'income' );
```
Então aplicando o método filter no array "transaction", ele percorre elemento por elemento, testando se a transação é do tipo "income", caso seja verdade, ele será salvo dentro um novo array.

Agora que temos um novo array só do tipo "income", podemos fazer a soma com o reduce.

```js
const sumIncome = filteredIncome.reduce(
(sum, currentValue) => {sum + currenteValue.value});
```
Agora já temos o valor da soma de todos o itens no para "income".

Entretanto nossa ultima função tem um pequeno problema, para quando tivermos apenas uma unica transação, de um único tipo.

Em vez de retornar a soma, ela apenas irá retornar o objeto.

Para corrigir isso, incluímos um valor inicial, da seguinte forma,
```js
const sumIncome = filteredIncome.reduce(
   (sum, currentValue) => {
     return sum + currenteValue.value
}, 0);
```
Desta forma, mesmo se tivermos apenas um "income" ou "outcome" o valor da soma estará correto.

Basicamente terminamos, basta fazer a mesma coisa para as transações do tipo "outcome" e então retornar os valores com o saldo.

Entretanto, podemos deixar o código um pouco mais elegante e menor. Ao invés de usar duas variáveis, podemos utilizar somente uma, aplicando filter e reduce, um após o outro.

```js
const income = transactions
  .filter( transaction => transaction.type === 'income')
  .reduce((sum, currentValue) => {
     return sum + currenteValue.value
}, 0);
```
Agora basta fazer o mesmo para "outcome" e criar uma função que retorna os valores com o saldo calculado.

```js
function getBalance() {
  const income = .....
  const outcome = .....

  return {
   income,
   outcome,
   balance: income - outcome,
  };
}
```

Código completo: [gitHub](https://github.com/ArielKollross/ProgrammingArticles/blob/master/JsArticles/getBalance.js)

#Referências

[Reduce](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce) - acessado em 5/06/2020

[Filter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) - acessado em 5/06/2020
Group By Size
Tags: beginners
Published: 2020-07-06 01:16:06

I worked on a fun LeetCode problem earlier this week and wanted to discuss the problem here.

The problem is called ['Group the People Given the Group Size They Belong To'](https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/).

The problem tells us that there should be a function that accepts an array of integers that represent the size of group that each element belongs to. We are asked to return one example of an acceptable  grouping (with each element in the output represented by the index in the original array). For example:

An input of `[1, 2, 4, 2, 4, 4, 4] could return [[0], [1, 3], [2, 4, 5, 6]`. This is an acceptable return because when examining the input, index 0 is in a group of 1 so it should be grouped alone. Indexes 1 and 3 are both in a group of 2, and indexes 2, 4, 5 and 6 are all in a group of 4. This example was straightforward because there were not multiple groups of the same size. If we received an input of `[2, 2, 2, 2, 2, 2, 2, 2]` then you could return any combination of four pairs of indexes. 

Here is my solution:

```javascript
function groupThePeople(groupSizes) {
    let groups = {};
    let result = [];
    for (let i = 0; i < groupSizes.length; i++) {
        let tar = groupSizes[i];
        if (tar === 1) {
            result.push([i]);
            continue;
        }
        if (!groups[tar]) {
            groups[tar] = [];
            groups[tar].push(i);
        } else {
            groups[tar].push(i);
            if (groups[tar].length === tar) {
                result.push(groups[tar]);
                groups[tar] = [];
            }
        }
    }
    return result;
}
```

I went about solving this problem by utilizing an object to store groups as they are being filled. My logic was that if I created a key for each group size and used this object as a staging place, I could push a value array into my final result array once it was full. I then reset that object key to an empty array so that it would be ready for future indexes that also have the same group size. 

My `for` loop iterates over each element of the input. If the element I am examining is `1`, I know that I can immediately push the index into my result array and skip to the next index in my for loop. 

The remaining code in my `for` loop carries out the logic to keep my staging object organized. 

Once we exit the `for` loop I know that we have placed each index into an appropriate group, so we return the result array (this problem did guarantee that there would be at least one solution for each input, so I did not account for invalid inputs; if I did want it to notify of an invalid input I would flatten the result array before the return and confirm that it was the same length as the input array).\

This was one way of solving this problem but I'm sure there are many more!
REST API Using Only Standard Go Packages
Tags: go, tutorial, video, rest
Published: 2020-07-06 01:11:25

{% youtube Z01gUyC-q0E %}

Code available [here](https://gitlab.com/dak425/go-rest-api-no-deps)

For my next go tutorial, I decided to actually demonstrate how to create a program. In this case, a REST API.

But not just any REST API, oh no. This one is built using only the standard package library available in go. While there are really nice resources for doing this, such as [gorilla/mux](https://github.com/gorilla/mux), I wanted to demonstrate the power of the standard package library in go.

Check it out and let me know what ya'll think. Any feedback is greatly appreciated.

If you liked it and want to know when I post more, be sure to [subscribe](https://www.youtube.com/channel/UCTHij3Ac5GizLsn5yB4IX_Q?sub_confirmation=1) and thank ya'll again for your time!
Enhancing Optimized PySpark Queries
Tags: python, datascience, spark
Published: 2020-07-06 01:04:41

As we continue increasing the volume of data we are processing and storing, and as the velocity of technological advances transforms from linear to logarithmic and from logarithmic to horizontally asymptotic, innovative approaches to improving the run-time of our software and analysis are necessary.

These necessitating innovative approaches include utilizing two very popular frameworks: Apache Spark and Apache Arrow. These two frameworks enable users to process large volumes of data in a distributive fashion. These two frameworks, also, enables users to process larger volumes of data more quickly by using vectorized approaches. These two frameworks can easily facilitate big-data analysis. However, despite these two frameworks and their ability to empower users, there is still room for improvement, specifically within the python-ecosystem. Why can we confidently identify pockets of improvement in utilizing these frameworks within python? Let’s examine some features python has.

...

If you want to learn more, please continue reading here: https://towardsdatascience.com/enhancing-optimized-pyspark-queries-1d2e9685d882
Data Preprocessing And Visualization In C++
Tags: cpp, machinelearning, datascience
Published: 2020-07-06 01:00:38

Data preprocessing is the process of converting raw data into computer understandable formats, it’s the first step in any machine learning operation. Data collection is usually loosely controlled and may result in out-of-range values. Data preparation and filtering steps can take a considerable amount of processing time.
Data preprocessing includes:

   * Reading Data from files.
   * Data cleaning.
   * Instance selection.
   * Data standardization.
   * Data transformation.
   * Feature extraction and selection.

The product of data preprocessing is the final training set. In this article, I will address some of the data preprocessing steps while using C++, also data visualization using the Matplotlib-Cpp library.

First, this article is part of a series discussing the implementation of the Machine learning Basics using C++. Please follow 

In this article, I will use the iris dataset as an example of the data that we can perform each operation on it, also note that I will be using C++11 in this tutorial.

##Reading Data from Files:

After downloading the iris.data file from [here](https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data). let’s read the data from a file with a simple read file instructions and parse each type of data in a separate vector.

[Link to the original article](https://towardsdatascience.com/data-preprocessing-and-visualization-in-c-6d97ed236f3b?source=friends_link&sk=d13cdabff65a5ce56b717835108615ab)
Scaling DAG Creation With Apache Airflow
Tags: python, architecture
Published: 2020-07-06 00:56:10

One of the more difficult tasks within the Data Science community is not designing a model to a well-constructed business problem or developing the code-base to operate in a scalable environment, but, rather, arranging the tasks in the ETL, or in the Data Science pipeline, executing the model on a periodic basis and automating everything in-between.

This is where Apache Airflow comes to the rescue! With the Airflow UI to display the task in a graph form, and with the ability to programmatically define your workflow to increase traceability, it is much easier to define and configure your Data Science workflow in production.

One difficulty still remains, though. There are circumstances when the same modelling, monolithic, process is utilized and applied to different data sources. To increase performance, it is better to have each of these processes run concurrently, rather than add them to the same dag.

No problem, let us simply create a dag for each process, all with similar tasks, and schedule them to run at the same time. If we were to follow along software development principle DRY, is there a way to create multiple different dags with the same-type tasks without having to manually create them?

*Is there a way to create multiple different dags with the same-type tasks without having to manually create them?*

....

To read understand and read more regarding how to scale your Apache-Airflow DAGS, please continue reading here: https://towardsdatascience.com/scaling-dag-creation-with-apache-airflow-a7b34ba486ac
Making A Model Is Like Baking A Cake
Tags: python, datascience, machinelearning
Published: 2020-07-06 00:53:09

# The Types of Cakes Available

As we progress further in our modern era, the advances of Data Science and Technology continue to make marvel strides in various fields of study and practice. As a result of the vast applicability of Data Science and Technology, various different types of models have been constructed.
To name a few: Generalized Linear Models, Support-Vector Machines, K-Nearest Neighbor Algorithm, Gradient Boosting Decision Trees, Random Forest, and Neural Networks.
Given the volume of the data and the complexity of the interactions within the data, various Data Science specific packages have been developed in a few different languages. Within python, we have, not exclusively, sklearn, xgboost, lightgbm, pyspark and H2O. Within R, we have, not exclusively, Caret, Prophet, SparkR, and xgboost.

Each of the aforementioned packages attempts to solve very specific Data Science problems. However, given the nature of the initial problem, we may need a more custom approach deriving the solution. Therein lies the problem each Data Scientist strives to solve.

*Typically, due to the variety of the problems in the field, Data Scientists develops solutions using models, with predefined model architecture, and seeks to increase the performance of certain KPI metric performance the predefined model was not specifically made to solve.*

Fortunately, there is a way to develop a modelling architecture to specifically solve your problem. By understanding the mathematical and statistical processes each of these predefined model architecture uses, it is possible to reverse engineer the model to specifically your problem.
First, let us examine one family of models, and then proceed to developing our own model....

To read understand and read more regarding how to create your own special unique ML custom model, please continue reading here: https://towardsdatascience.com/making-a-model-is-like-baking-a-cake-5f2443894c5f
When Should You Learn Machine Learning using C++?
Tags: machinelearning, cpp, datascience
Published: 2020-07-06 00:49:36

a guide for the Scientist, Developer and User of Machine Learning.

A while ago, I decided to start implementing Machine learning using C++ to enhance my skills in both Machine learning and C++. I searched for a good online tutorial to guide me through the implementation, but the fact is there are not so many tutorials that can help you learn these two together. In fact 99% of online Machine learning tutorials I found were using Python, R or Matlab.


I agree that Learning both C++ and Machine learning is a very difficult mission, but as I said before, the target here was to learn both together.

I looked for a reason why most of the people recommend using python while learning ML not C++, and here is the summary:

* Python is a more flexible language than C++.
* The library functions make the task trivially simple.
* To work interactively with data by just running the Python interpreter in the command line.
* The main purpose is to learn about ML, not to practice programming.
* Debugging C++ code for ML algorithms is very difficult.

On the other hand, reasons support learning how to Implement ML in C++ are:
* C++ is more efficient than most other languages.
* You can control each single resources starting from memory, CPU and much other things.
* Most frameworks are implemented in C++under the hood, like TensorFlow, caffe, vowpal, wabbit and libsvm.
* Learning machine learning in C++ makes you a very desirable hire target.

Most of the answers was directed toward people who are new to Machine learning, on the other hand I was searching for more detailed answer.

People working on machine Learning can be divided into three categories:
[Link to the original Story](https://medium.com/ml2b/when-should-you-learn-machine-learning-using-c-6edd719f95ff?source=friends_link&sk=8b66a55c047b99bbd1c720e59fe6a770)
Binary Search Trees, Explained! (Intro)
Tags: javascript, algorithms, datastructures, trees
Published: 2020-07-06 00:25:47

## Intro

**Binary Search Trees**; If you Google the name, you'll see that they have a lot of nicknames such as BSTs, Ordered Binary Trees, Sorted Binary Trees, etc. 

No matter what name we choose, we can all agree that a Binary Search Tree sounds like a super-complex computer science term that only programming Gods and math geniuses are able to understand!

<img src="https://media.giphy.com/media/3o7btPCcdNniyf0ArS/giphy.gif" />

But, just like a lot of other "scary" or complex-sounding programming and tech terms, Binary Search Trees are actually very interesting and not that hard to learn, implement and work with! 

A Binary Search Tree (I'll call them BST(s) from here on) is a type of Binary Tree which itself is a type of Data Structure. Data Structures are ways of organizing and accessing data when we're working with computers and writing programs. If you've ever used a language like JavaScript, chances are, you've been using basic data structures without even realizing you are. Arrays and objects are two types of basic (and helpful!) data structures that you can find in almost any programming language. 

<img src="https://media.giphy.com/media/1SvtUkj2T6122rQ7oX/giphy.gif" />

In the upcoming article(s), I'll go over what BSTs are and how we can implement our own BST in JavaScript! 

Whether you're an absolute beginner and this is the first time you're hearing about BSTs, a more experienced developer who just needs a reminder on BSTs or someone who's preparing for a technical interview, these articles are for you!

If you think more people can benefit from reading my series of posts on BST(s), share this post with your friends! I appreciate all of the support. 

Stay tuned for the next post!
FCC: Portfolio Project
Tags: codepen, html, css, beginners
Published: 2020-07-06 00:21:50

{% codepen https://codepen.io/notalexpareja/pen/BajYpPQ %}

After completing Free Code Camp's front end course, there are a few things I took away from the experience. 

1. I have more questions about web development than before I started. Which means;

2. I got better at googling questions about issues I had. Some issues are hard to explain. It feels like google searching is a skill in itself.

3. This one is important. I put ~300 hours worth of really putting my heart into learning something new.

I proved to myself that I can "grasp it", even just a little. What was once an intimidating soup of colorful letters is now familiar and readable. 

Moving onward, I will be humbling myself with FCC's Javascript courses. This is next level stuff for me, so I'm not trying to rush it.

Slow and steady wins the race.
t-minus v2.0 - timers/setTimeout with superpowers (a rewrite of my oldest side-project)
Tags: showdev, npm, contributorswanted
Published: 2020-07-06 00:12:28

Earlier this year I decided to take my oldest side project and rewrite it with what I know now.

t-minus v1.0 was written in 2015 as an easy way to set timers, with some emphasis on use in a browser UI.

Specifically, it was born out of an even larger side project that uses pomodoro timers (eattomatoes.herokuapp.com - unmaintained but still somewhat functional)

It utilized callback functions to control what happens as seconds pass, as well as when the timer hits 0.

```javascript
var timer = new Timer('1:30', {
  onInterval: function() {
    this.getTimerUI(); // human readable string!
  },
  onTimeout: function() {
    // times up!
    this.restart(); // `this` refers to its own timer + handy methods!
  }
});
```

It had all the methods you'd expect on a timer.

`timer.pause`
`timer.resume`
`timer.reset`
`timer.clear`

_and it was written as a single vanilla JavaScript file_

## The 2.0 rewrite

```javascript
const { Timer } = require('t-minus');
```

I rewrote the entire module from scratch, taking into account v1's README to preserve the interface. I wanted a better written package that's more optimal, but had the same basic API and usage as its original.

The end result has some new features for use:

* Support for milliseconds (v1's smallest unit was essentially the second)
* UI/timer setting units up to years (v1's largest unit was the hour)
* More accurate pausing (v1's pause would stop at the last full second)
* More memory efficient (v1 would keep setIntervals when a timer is paused; not in v2)

...and new features for development.

* Unit test coverage 🙌
* Written in TypeScript 👨‍💻
* Overall better documented.


If you need timer functions or want a better way to handle countdowns in the browser (or node). Consider giving t-minus an install 😁

If this project gets even one contribution from someone other than me, I would have exceeded my goals with it. [Check out the repo if you feel so inclined](https://github.com/nickpalenchar/t-minus)
Four Reasons For Having a Blog
Tags: blogging, writing, learning
Published: 2020-07-06 00:06:12

Why should you, a busy developer, put time and energy into having your own blog?

- Writing enforces understanding.

- There's satisfaction in "shipping" each post.

- You can reference your own posts when you need to remember how to do something later on.

- Writing well is like a developer superpower, your blog is where you practice and get the reps in. :muscle:


__Not every post needs to be long__.  You're probably glad this post is already over.

> Hopefully you found this post helpful, if you have any questions you can find me [on Twitter](https://twitter.com/wes_goulet).

_Header image from [undraw](https://undraw.co/)_
The for...in vs for...of statement in JavaScript
Tags: javascript
Published: 2020-07-06 00:00:00

---
  title: The for...in vs for...of statement in JavaScript
  published: true
  tags: JavaScript
  date: Mon Jul 06 2020 05:30:00 GMT+0530 (India Standard Time)
  canonical_url: https://melvingeorge.me/blog/for-in-vs-for-of
  cover_image: https://melvingeorge.me/_next/static/images/main-5a6e1c9e908a0c251d6843b0149b3f12.jpg
  ---
  [Originally posted here!](https://melvingeorge.me/blog/for-in-vs-for-of)
  
![spiraling stairs](https://melvingeorge.me/_next/static/images/main-5a6e1c9e908a0c251d6843b0149b3f12.jpg)

<p class="text-center">
  Photo by
  <a href="https://unsplash.com/photos/u2d0BPZFXOY" target="blank">
    Tine Ivanič
  </a>
</p>

## `for...in` statement

The `for...in` statement is used to loop over object enumerable properties.

Let's consider an object called `john` with `name` and `age` fields

```javascript
// object
const john = {
  name: "John Doe",
  age: 23,
};

// loop over object
for (let key in john) {
  console.log(key); // Result: "name", "age"
}
```

If we loop over an array it will loop over the index value since the arrays are objects in their underlying nature.

```javascript
// array
const john = ["John Doe", 23];

// loop over array
for (let key in john) {
  console.log(key); // Result: "0" , "1"
}
```

## `for...of` statement

These are a little different from the `for...in` statement.

The `for...of` statement is used to loop over the values of iterable objects only which includes Arrays, Maps, etc.

The `for...of` statement gives out the values instead of indexes.

If we try to loop over objects it will return an error.

Let's try to loop over an object using the `for...of` statement.

```javascript
// object
const john = {
  name: "John Doe",
  age: 23,
};

// loop over object
for (let key of john) {
  console.log(key); // Error
}
```

Now let's loop over an array

```javascript
// array
const john = ["John Doe", 23];

// loop over array values
for (let value of john) {
  console.log(value); // Result: "John Doe" , 23
}
```

### Feel free to share if you found this useful 😃.

---