teleren/devto huggingface dataset:
Java: Nesting functions with Stream Reduce
Tags: functional, java
Published: 2019-04-18 02:56:20

---
title: Java: Nesting functions with Stream Reduce
published: true
description: Nesting functions in functional way.
tags: #Functional Programming #Java


---



# Java: Nesting Functions with Stream Reduce
[fold]: <https://wiki.haskell.org/Fold>
[Identity Function]: <https://en.wikipedia.org/wiki/Identity_function>
One of the less known stream operators is _Reduce_. I've studyied Haskell during University and _reduce_ strongly reminds of Haskell's function _[fold]_.

#           What is _reduce_ ?
_Given a list of elements and a function (aka **accumulator**), get a single resulting value._
The easy example: make the sum of a list of Integers.
`Integer result = Stream.of(1, 2, 3, 4, 5).reduce(0, (a, b) -> a + b);`
That will make: **1+2+3+4+5+0**

However, Streams api already solves that:
`Integer result = IntStream.of(1, 2, 3, 4, 5).sum();`

# Composite functions with Reduce
Suppose we have a list of functions and we want to build a function which contains all the ones from the list. Let see:
First we have:
`List<Function> functions = [f1,f2,f3,f4,f5]`
And we want a function like this:
` Function resultFunction = (X) -> f5( f4( f3 ( f2( f1(X) ) ) ) `

## How can we easily build a composite function ? 
First, we'll use _Function::andThen_ method to wrap each function in another.
Then, we call _reduce_ and the first argument is the _starting value_, as we are composing functions we use the **idempotent** element which, in this case, is the [Identity Function]  (a -> a).

     List<Function> functions = [f1,f2,f3,f4,f5];
     Function compositeFunction = functions.stream().reduce(a -> a, Function::andThen)

**What if we want to composite in the reverse order of the list ?**
Remember the array is:
`List<Function> functions = [f1,f2,f3,f4,f5]`
And now we need a function like this:
` Function resultFunction = (X) -> f1( f2( f3 ( f4( f5(X) ) ) ) `

We have to change the _accumulator_ to Function::compose

     List<Function> functions = [f1,f2,f3,f4,f5];
     Function compositeFunction = functions.stream().reduce(a -> a, Function::compose)
     

Altogether _Reduce_ , _Function::compose_ and _Function::andThen_ conceive a powerfull tool to compose functions which is of great use.
Super fast full text search with PostgreSQL
Tags: showdev, blog
Published: 2019-04-18 02:30:24

---
title: Super fast full text search with PostgreSQL
published: true
description: Search tens of millions of records in seconds b
tags: showdev, blog
---

This was [originally posted oh my blog where I post all things tech and sysadmin and the like!] (https://kuby.ca/fast-full-text-search-using-postgresql/). 


What if I told you that searching millions of records didn't have to be complex, and take minutes to complete? Most organizations jump ship to a dedicated searching engine like Elastic Search, but they greatly underestimate the power of PostgreSQL.

Let's suppose we have a table with 1.1 million records, and we want to apply a text search vector to it. We care about the author and publisher names. In order to create the vectors, we need to take out old data, and put it into a new column that has a vector.


Time to break it down! Our author and publisher names are just strings, for example author is "Jack Sparrow", publisher is "A Movie Corporation" - we want to be able to start typing "jac" or "mov cor" and have it pickup Jack Sparrow and/or A Movie Corporation. Makes sense! The issue we face is we don't want to have two columns, we need an index so we can rapidly search, so we make a new column in the database table, let's call this column "tsv" for short.


First, add the column, then apply a GIN index on it.

```SQL 
ALTER TABLE searchable ADD COLUMN tsv tsvector;

CREATE INDEX searchable_gin_idx ON searchable USING GIN(tsv);
```

Now, let's update our table records accordingly, you can use this SQL to create an English TSVector and instantly apply it to the TSV field.

```UPDATE searchable SET tsv = ( to_tsvector('english', author) || to_tsvector('english', publisher ) );```

This will begin to build a simple GIN index using our concatenated author and publisher. We will now be able to perform both simple and complex fast search queries. It's the same data, without compromising speed. For example, if we want to find all items containing 'jac parr' as the author, we can do:

```SELECT * FROM searchable WHERE tsv @@ to_tsquery('jac:* parr:*');```

Instantly, we're presented with a ton of results. It's interesting to note, that even though we've attached a wildcard to the query, PostgreSQL is still able to rapidly search and get our result set. We could further apply a ts_rank_cd in the ORDER BY clause to rank them according to weights.

Speed and consistency matters.
If we run explain analyze on it, we can see that the query took 0.013ms to plan, and 0.067ms to execute.

If we were to run the same query without a GIN index, we see it took 0.121ms to plan, and 132.14ms to execute.

As you can see, this is a major performance boost, and we get the same results we did previously, much faster this time around. Thus, another reason you may not need a searching product - if you optimize your queries and database table with the proper indexes you can leverage the full power of PostgreSQL.
You Can't Escape The Mesh
Tags: 3d, threejs, javascript
Published: 2019-04-18 01:14:50

---
title: You Can't Escape The Mesh
published: true
description: The creation of a simple 3D chamber navigation exercise, inspired by the great computer game Descent
tags: 3D, ThreeJS, Javascript
cover_image: https://thepracticaldev.s3.amazonaws.com/i/4ztadih6vm8fbvxixppg.png
---

### Introduction

When I first began studying web development several years ago I was attracted to 3D graphics, having come from an architecture background where I did a lot of work in 3DsMax, and later Revit. I ended up doing a group project that combined my love of 3D with motion tracking using the Leap Motion tracker. The result was a 3D hand controlled version of the classic game Pong, built using the ThreeJS library. My interest in 3D has persisted, through playing games like Portal. Eventually I decided it was time to try some more development. Question was: where to begin?

[You can read my article about creating Crazy Leap Pong here](http://moremooredesign.com/crazy-leap-pong/)

### Rediscovering the joy of Descent

One of the first computer games I played intensely was the 3D shooter game Descent. This was where I was introduced to the concept of portal rendering, only rendering what needs to be rendered in 3D. Descent handles this concept quite nicely since the setting consists of mines with multiple chambers. If you're in one chamber and there's a door, there's no need to render the next chamber until you open the door. I decided that this would be a good exercise for creating a navigable 3D experience. My previous work had been simple simulations of objects in motion.

### Creating the opening scene

I decided to create a simple chamber to chamber environment, where the two chambers would be linked to a tunnel that would only be revealed after a door was opened. I quickly realized that cubes in 3D have no "solidity" unless you specify that they do; otherwise, they are simply objects with six sides that you can't see through. Therefore, I simply created a cube and placed my camera inside of it.

### Open, Sesame

I handled collision detection by using the ThreeJS Raycaster to determine if the direction of travel would intersect one of the sides of the surrounding cube. In order to escape the chamber, the user would need to navigate to the door and press a key.

This action would trigger the rendering of the rest of the environment: the tunnel and the other chamber. Until this point the starting chamber was the extent of the world. I used another library to handle the union operation to combine the newly rendered tunnel and destination chamber with the starting chamber into one mesh.

### Demo

The CodePen is below. Feel free to explore.

{% codepen https://codepen.io/moremooredesign/pen/wpaPra %}
Error Handling: Address In Use
Tags: errors, node
Published: 2019-04-24 00:05:52

---
title: Error Handling: Address In Use
published: true
tags: ['error handling', 'node']
canonical_url: https://stephencharlesweiss.com//2019-04-18/error-handling-address-in-use/
---

Running my node server, I got an error indicating that my port was already in use (`Error: listen EADDRINUSE :::5001`).

I knew that _shouldn’t_ have been the case because I had only one application running on that port.

[![error message](https://stephencharlesweiss.com/static/dd92eccd789708159abf2c23b73c2f44/dffc8/error-message.png)](/static/dd92eccd789708159abf2c23b73c2f44/dffc8/error-message.png)

Still, killing the process multiple times from the console didn’t work, so I went in search of the process ID (PID) to kill it specifically.

There are multiple ways to do this, but the one that worked for me out of the box (on Mac) was `lsof` which is the “list open files” program.

The `-i` flag allows for inclusion of specific ports.

In my case, I was interested in port `5001`, so the command was `lsof -i :5001`.

This returned exactly what I needed: [![lsof example](https://stephencharlesweiss.com/static/a50d83d346bf97afbd88c54a78223792/fb8a0/lsof-example.png)](/static/a50d83d346bf97afbd88c54a78223792/fca27/lsof-example.png)

Apparently the `node` server hadn’t terminated and was still using the port, but now I knew the PID. This meant I could hop over to Activity Monitor, look it up and kill it. [![kill activity](https://stephencharlesweiss.com/static/014cc7352826b2859174b646310f49ff/fb8a0/kill-activity.png)](/static/014cc7352826b2859174b646310f49ff/7636c/kill-activity.png)

Now when I restarted the server it worked like a charm.

# Resources

- [3 Ways to Find Out Which Process Listening on a Particular Port](https://www.tecmint.com/find-out-which-process-listening-on-a-particular-port/)
Ascii Glitch Animation with js
Tags: javascript, animation, github, library
Published: 2019-04-17 23:40:32

---
title: Ascii Glitch Animation with js
published: true
description: A small library to animate ascii characters
tags: js,javascript,animation,github,library
---

![IMG](https://thepracticaldev.s3.amazonaws.com/i/fsg7jchv0mezcfuyh79i.gif)

I've been working on this for a bioinformatics project, turns out it can also be used to animate ascii characters as well:P.

- [Example](https://observablehq.com/@ibrahimtanyalcin/ascii-warp-github)
- [Another example](https://distreau.com/lexicon-mono-seq/examples/example-5.html)

And here is the library:
https://github.com/IbrahimTanyalcin/lexicon-mono-seq

I'd really appreciate bug reports.
Tackling the Terror of the Tech Talk - Part 1: Introduction
Tags: techtalks
Published: 2019-04-17 23:21:56

---
title: Tackling the Terror of the Tech Talk - Part 1: Introduction
published: true
description: Some tips to help you give the type of talk you would actually want to hear.
tags: #techtalks
cover_image: https://thepracticaldev.s3.amazonaws.com/i/jxorxasj2iicdbmqyg5s.jpg
series: Tackling the Terror of the Tech Talk
---

### An ~~un~~Foreseen Nightmare
The scene plays itself over and over across meetups all around the globe. The local meetup organizer, trying their best to offer interesting topics and meaningful experiences, approaches a developer and asks,
> Would you be willing to present at the meetup next month?

It is typically at this point many developers find the next meetup time is a perfect opportunity to schedule that root canal they have been putting off. 

It is not that people are unwilling to help, in fact often the opposite is true. If that same organizer asked individuals to help set up tables, coordinate logistics or prepare some sample code; he or she will likely find willing participants. However when it comes to presenting, the mere idea seems to spark an innate dread or even fear.

Now to be fair, this is in no way isolated to the tech industry. Various reputable sources annually study the most common fears of individuals [(here is one such example)](https://www.washingtonpost.com/news/wonk/wp/2014/10/30/clowns-are-twice-as-scary-to-democrats-as-they-are-to-republicans/?noredirect=on&utm_term=.1a48d7117001) and public speaking is always near the top. The reality is most all of us will hesitate when asked to speak in front of others for reasons such as:
> What am I going to say?
> What if I say something wrong and look like an idiot?
> What could I even talk about that these people do not already know?
> What if I am....boring???

My goal in this series of articles is to provide for you a **basic** framework to help guide you through:

* How to structure your talk
* How to present your talk
* How to ensure you are *adding to* rather than *taking away from* your talk

Will a few articles on a website get you an invite to the next major dev conference? Probably not. However my hope is that these articles can provide you the structure and build your confidence to tackle your first (or next) tech talk.
State of DevRel 2019
Tags: devrel, community, opensource
Published: 2019-04-17 21:27:18

---
title: State of DevRel 2019
published: true
description: 
tags: devrel, community, opensource
---

Think about what makes an API, language, or framework really useful. I mean, what really sets two similar tools apart?

Is it some obscure feature? Maybe the docs? Or is it the developer community that surrounds it?

More and more companies now have teams dedicated to making sure their developer-targeted products have a healthy community. Those teams often go by the name of developer relations (dev rel) or developer advocacy.

We at Hoopy have produced a [report into developer relations](https://stateofdevrel.hoopy.io) as it is today in 2019 and here are some highlights.

# Where are dev rel people?

Developer relations is an increasingly global profession:

 * 52% of dev rel professionals are in the US
 * 15% are in the UK
 * there’s at least one dev rel practitioner in 59 countries.

You can now find people whose job it is to help developer communities on every continent; other than Antarctica, of course.

# What do dev rel people do?

Developer relations people usually work on anything that helps to build awareness, adoption, and community around a developer-targeted product.

Typically, they tend to:

 * speak at meet-ups and conferences
 * write sample apps, demos, docs, and blog posts
 * hang out in Slack, StackOverflow, and other channels to help developers
 * organise initiatives like champion programmes.

# What team are they in?

The department within which dev rel sits can shape the expectations, metrics, and make-up of the programme. 

This year, we found that 35% of dev rel teams report into marketing and 16% act as standalone dev rel departments.

# How big are dev rel teams?

Most dev rel teams have between two and five members. In fact, more than three quarters of dev rel people work in teams of ten members or fewer.

However, the number of larger teams is growing. Companies such as IBM and Google have enormous teams that span the globe.

# Where can I read more?

The full [State of Developer Relations 2019 report](https://stateofdevrel.hoopy.io) is available for anyone to download free of charge. If you're interested in knowing more about dev rel in general, start at the [DevRel website](https://devrel.net)!
Why you should care when you are sending the notifications?
Tags: notifications, twitter, newsletters
Published: 2019-04-17 20:42:22

---
title: Why you should care when you are sending the notifications?
published: true
description: 
tags: notifications, twitter, newsletters
cover_image: https://thepracticaldev.s3.amazonaws.com/i/azapbiyaimdhh37mr9yb.png
canonical_url: https://lyubomir-angelov.com/why-you-should-care-when-you-are-sending-the-notifications/
---

What I observe on vast of the majority of the news websites and not only them, is that they are sending all kind of notifications (Apps, Twitter, Emails etc.) when possible, 10 AM, 2 PM, 11 PM, 1 AM.
That's not right, know your audience, look at your analytics, check most valuable hours, think when **you** want those notifications, ask friends etc.

If you are not thinking about that, not knowing your audience, it is time to start doing that.

Why? Because you can disappoint your users, for example when you publish something to Twitter or you send an email at 1 AM.
Believe me, users will stop their subscription to your Twitter tweets or will unsubscribe from your newsletter and they will not read. You are not leaving a good impression like that.

In general try to think as a real customer, ask friends and make A/B tests.
Send one email, ok at 1 AM to **half** of your users, then check how many of them opened it, read it and clicked on the links in, then do the same with the other HALF but in a different time, for example, 9 PM.

All big websites are doing that, why you are not doing it?
Learning R: Rant 1
Tags: r, datascience, machinelearning, lifescience
Published: 2019-04-17 19:44:08

---
title: Learning R: Rant 1
published: true
description: 
tags: #R, #datascience, #machinelearning, #lifescience
---
I learned machine learning and data science last year with Python in my previous job, and this year I am more or less doing the same stuff in R for my current job. Learning R seems to be a pain right now, because to me coming from Python R appears to be a giant mess. There is n number of packages with multiple of them doing more or less the same thing. For example, right now I am learning about data frames/tables in R. So you have traditional R data frames that are not very easy to work with. Therefore, there is an enhanced version of it in the form of data.tables(). Former generates a data frame and the later generates a data table. In addition to the data frame and table, there is something else called tibble that is produced by the packages in the tidyverse library. Why there is not just one package like Pandas for data frames and Numpy for nd arrays?
Time till Endgame
Tags: react, project, beginners, marvel
Published: 2019-04-17 19:36:47

---
title: Time till Endgame
published: true
description: A small project I made following a joke from my coworkers.
tags: react, project, beginner, marvel
cover_image: https://thepracticaldev.s3.amazonaws.com/i/85lnm6fo2kbh8fgpsryy.png
---
# Why?
My coworkers and I are great Marvel fans, so naturally we can't stop talking about Endgame, the movie which is going to be premiered soon.

Everyday we ask eachother how much time do we have to wait to go to the cinema to finally watch it, so I decided to make a webpage in which you insert the day you are watching it and it returns you a counter which days and time left to watch it.


## Links

### v1
You can see the website [here](https://time-till-endgame.glitch.me/). If you wish to see the code, you can see it on my [Github repository](https://github.com/LMOlivera/time-till-endgame). It was made with React in a day and a half, so don't expect a lot of functionalities, but spaghetti code and bad UX.

**EDIT**: Glitch is having some maintenance, so the site may not be available!


### v2
- [Online](https://time-till-endgame-v2.glitch.me/)
- [Github repository](https://github.com/LMOlivera/time-till-endgame-v2)
This version allows you to see who is going to watch the film, when and if they can spoil you (or if you can spoil them). Also, let's you post on Twitter how much time you have to wait to finally see the film.



Feel free to give me advice about anything that I can improve! But do it before Endgame premieres!


*Thank you for reading. Don't forget to follow me on dev.to and Twitter!*
Have anyone managed to format code in IntelliJ just like Eclipse?
Tags: help
Published: 2019-04-17 18:33:33

---
title: Have anyone managed to format code in IntelliJ just like Eclipse?
published: true
tags: help
cover_image : https://thepracticaldev.s3.amazonaws.com/i/gh7w8h328tocso6hx85f.jpeg
---

My whole team is working with Eclipse. I am the only solo adventures who prefer IntelliJ.😎

And you all must know that the whole team has to use the same formatting rules. Other than that merging will be a nightmare.

Have anyone of you guys managed to use eclipse Formatter inside IntelliJ?

I tried some guides online, both didn't really work
1. I tried to export Eclipse formatting options and import it into IntelliJ. That for some bizarre reason didn't work. they must have different sets of options.
2. I tried to use an IntelliJ plugin called Eclipse Formatter but it throws an error. It's not even formatting the code for me
3. I tried to go over the options one by one in both IDEs. but gave up quickly as there are lots of options and not all are very clear to what they do.

I have been using eclipse to format my code after each task for a while now. please heeelp! 😊😀

And please don't tell me to switch to Eclipse. because I really like IntelliJ and got used to it.
PHP Dev Environment One-Liner
Tags: php, webdev, productivity, beginners
Published: 2019-04-17 18:26:39

---
title: PHP Dev Environment One-Liner
published: true
description: 
tags: php, webdev,productivity, beginners
---
Here's the fastest way to get your PHP app running.  No MAMP, WAMP, apache or any of that nonsense.  

Moreover, it allows you to run multiple projects independently.

I'm assuming you have docker.


### tl;dr
This runs the php docker image, mounts the current directory, and spins up a server on port 8086

```
$ docker run -v $(pwd):/www -it -p8086:8086  php:5.6-alpine sh -c "cd www; php -S 0.0.0.0:8086"
```

### The Full Version
#### Create your index.php
```
$ cat > index.php
<html><body><h1><?php print("Hello World!") ?> </h1></body></html>
CTRL-D
```


#### Run the Server
```
$ docker run -v $(pwd):/www -it -p8086:8086  php:5.6-alpine sh -c "cd www; php -S 0.0.0.0:8086"
```

#### Test Your Server

```
$ curl localhost:8086
<html><body><h1>Hello World! </h1></body></html>
```
Thoughts on migrating to TypeScript and improving the overall quality of the frontend DEV codebase
Tags: javascript, typescript, meta, opensource
Published: 2019-04-17 18:14:12

I think the evolution and increased popularity of TypeScript over the past year or so means that it is a good time to migrate towards using it on DEV.

Nick Taylor has been the biggest proponent, but it all jives with my view of things.

Read more here:

{% link https://dev.to/nickytonline/dev-to-with-a-typescript-or-flow-frontend-codebase-1n33 %}

I did not want us to be too far on the bleeding edge of frontend technology and that continues to be my stance, but I think TypeScript is in a good place and Rails' support of "modern" JavaScript is likewise in a good place.

As it currently stands, we have some of our JavaScript in the "old" `app/assets/javascripts` area of the codebase and some in the new `app/javascript` area which is configured via Webpacker.

As mentioned by others, I don't think we need to wholely re-write in TypeScript, but we should begin refactoring and writing new features this way. And in general, we should definitely be migrating the oldest JavaScript into the new area to whatever extent possible.

I think this initiative can pretty effectively be led by the community rather than top down from our team, because we won't need to provide a lot of specific instruction in order to make improvements in these areas.

Any pull requests to provide good instructions for this approach in the README and/or docs would be appreciated to get the ball rolling on this.

Part of the reason I feel like we can get moving on some of this now is that GitPod might be useful for helping frontend developers get up and running with the codebase and making changes without the worry of installing and getting the app running locally.

{% link https://dev.to/ben/spin-up-a-local-instance-of-dev-in-the-cloud-with-gitpod-it-s-incredibly-simple-pij %}

Only time will tell if GitPod leads to true productivity enhancements for frontend developers looking to contribute, but I think it's something worth exploring.