Skip to content

Instantly share code, notes, and snippets.

View armw4's full-sized avatar
🎯
Focusing

Antwan R. Wimberly armw4

🎯
Focusing
  • Stay tuned...
  • Atlanta, GA, USA
View GitHub Profile
@armw4
armw4 / array-flatten.js
Last active December 2, 2016 00:50
Integer flatten example while taking depth into account (via node version v7.1.0)
const flattenInternal = (input, initialDepth, acculamator = [], depth = 0) => {
if (initialDepth !== undefined && depth > initialDepth) {
throw new Error(`array has exceeded the target depth of ${initialDepth}.`)
}
return input.reduce((acc, value) => {
if (Array.isArray(value)) {
if (initialDepth !== undefined) {
return flattenInternal(value, initialDepth, acc, depth + 1)
}
@armw4
armw4 / mult-table.rb
Created November 10, 2016 13:33
Prints out a multiplication table. Written in Ruby for brevity. .NET equivalents in comments.
## To execute, install Ruby, download file, and run:
# ruby mult-table.rb
THREE_SPACES = ' ' * 3 # new String(' ', 3)
def print_header(rows)
print THREE_SPACES # Console.Write(...)
1.upto(rows) { |number| print "#{THREE_SPACES} #{number}" } # String.Format(...)
puts # Console.WriteLine(...)
end
@armw4
armw4 / when-to-presentational.md
Last active July 14, 2019 01:00
Apply presentational components where they make sense

This is a Judgement Call...but not a Difficult One

If you're a resident of redux land, you've probably heard the tale of container components vs presentational components. The latter has no knowledge of redux, does the heavy lifting rendering wise, and communicates with the former via callbacks. The former knows about redux, state, actions, the store, etc. There's a clear demarcation between the two sides. That being said, sometimes you don't need presentational components. If the rendering is simple enough, you can "just do it". You hear a recommendation, guide line, or best practice suggested by a framework author, and over time you apply it rigorously; treating it as a fact of life. I'm guilty as well (although I go out of my way not to be). Ultimately you have to be pragmatic about how you apply idioms and best practices. Sometimes it's worth asking "does this really make sense". I found myself wondering just that when I was staring at a container component that was a mirror image of the present

@armw4
armw4 / redux-is-smarter-than-you-think.md
Last active June 19, 2021 20:10
Optimizing your react components via redux....shouldComponentUpdate for free and more...

The Beginning

Yes...it's true...redux is smart....smarter than you even know. It really does want to help you. It strives to be sane and easy to reason about. With that being said, redux gives you optimizations for free that you probably were completely unaware of.

connect()

connect is the most important thing in redux land IMO. This is where you tie the knot between redux and your underlying components. You usually take state and propogate it down your component hiearchy in the form of props. From there, presentational

@armw4
armw4 / number-closest-to-average.md
Last active May 18, 2016 23:49
Question 5/6 of my Latest Programming Assessment

I only had 45 minutes in total to finish the entire assessment, but this was question 5/6. I ran out of time before submitting my final answer (which I'm disappointed in). Once the challenge was over, I thought more about the solution to the problem and was able to figure it out. What made the challenge more tricky is that the input was coming in from standard input and being buffered via the data event. Seeing this in node just kinda freaks you out when you're racing against time. All you can think is 😳. The authors of the assessment were kind enough to register the required event handlers for accumalating the input, and even provided the location of where to act on the final, fully buffered version of the input (I believe this was the 'end' event). Yes, stdin is a stream in node, and as a result, an event emitter. I'd still have run out of time even if I were able to fully derive my solution, as this took me at least 20 minutes of heuristics with a clear head, after the fact. That's pretty shocking.

@armw4
armw4 / streaming-request-node.md
Last active May 10, 2016 06:16
Streaming http resources with node via request

Streaming Resources in node With request From Source to Destination

The Beginning

If you've ever worked with http in node what you quickly come to realize is that it's not all that straightforward. You'll be bold, at least initially. Like me, you'll go read up on the docs for the http module. You'll properly subscribe to all the right events like on('data), and feel good about yourself. But if you're anything like be, you're bound do this wrong at some point. You probably won't subscribe to all the right things in the right order. You might not properly buffer the response. You may just have a WTF moment in production on day that brings down your entire site (just like I did about 2 years ago). Whatever the case may be, I believe you'll ultimately just settle on using request as your go to for http. If you want a promise based variant then you might go with some wrapper library as well.

So, we know request is the hip way of doing HTTP in node. request is kool. It's got a ton of usage and it pretty

@armw4
armw4 / index.js
Last active August 21, 2018 23:27
Coding exercise
import $http from 'http-as-promised'
import Promise from 'bluebird'
const resolveClass = (currentCount, { room, students, next }) => {
const studentsTwentyFiveOrOlder = students.filter(({ age }) => age > 24)
const count = currentCount + studentsTwentyFiveOrOlder.length
if (next) {
return $http
.get({
@armw4
armw4 / npm.md
Last active February 21, 2016 06:51
It's all about the last published date

Why?

Why...why are we here good sir? Well...I don't want to be here right now. I really don't. It's Saturday night, the Warriors beat the Clippers by the hair on the chinnies and dodged a late fourth quarter comeback, and I really want to eat. But...I have to blog. I have to do it now. If I don't, this story will not be the same. Why are we here? Because I'm on production support, that's why. I got an email from pagerduty that was like:

Unhandled rejection TypeError: next is not a function

I'm like huh? Wtf. What broke now? Why now? It's the weekend, I don't want to be here. I want to do me!!!!! But...I'm on production

Why?

Working with streams in node can be a scary and daunting task. The first time I saw the stream handbook, it left me quite perplexed. After seeing stuff like through, through2, and @acrute doing some stream kung fu on the job, I knew it was time. Streams are actually pretty straightforward once you get the hang of things. Read the handbook and the node docs and eventually your epiphany shall be born. I think one of the most confusing things for me was that Readable streams are for reading values that are produced by you, while Writeable streams are for dealing with values that are to be consumed by you. Say I wanted to put (write) some data from another stream into a file, and I wanted to take control of this process. Or say I just wanted to log (write) the results of a stream to say the console. These would be prime for Writeable

@armw4
armw4 / $httpBackend-url-matcher-builder.coffee
Last active August 29, 2015 14:15
Naive regex builder that helps henerate url matchers for $httpBackend based on routing syntax.
# https://docs.angularjs.org/api/ngMockE2E/service/$httpBackend
### @usage
path = require './path'
urlMatcher = path '/users/:guid/helloworld/:guid'
phones = [{name: 'phone1'}, {name: 'phone2'}]
$httpBackend.whenGET(urlMatcher).respond(phones)