Software Development and Programming Careers (Official Discussion Thread)

Joined
Jan 21, 2013
Messages
3,416
Reputation
514
Daps
7,621
Reppin
NULL
I'm probably shooting myself in the foot by avoiding redux (seems just as important as react) but I wrote my own state manager using a singleton class. I feel that the whole redux/flux architecture is convoluted bullshyt.

Repository.jsx
Code:
export default class Repository {

    constructor() {

        if( Repository.instance instanceof Repository ) {
            return Repository.instance ;
        }

        this.state = {
            counter: 0
        } ;
       
        this.subscribers = [] ;

        Object.freeze( this.counter ) ;
        Object.freeze( this ) ;
        Repository.instance = this ;
    }

    addSubscriber( component ) {
        this.subscribers.push( component ) ;
    }

    notifySubscribers() {
        this.subscribers.forEach( (value) => {
            value.forceUpdate() ;
        })
    }

    get(key) {
        return this.state[key] ;
    }   

    incrementCounter( n = 1 ) {
        this.notifySubscribers() ;

        this.state.counter += n ;
        return this.state.counter ;
    }
}

CounterDisplay.jsx
Code:
import React, { Component } from 'react'
import Repository from '../Repository'

export default class CounterDisplay extends Component {

    constructor(props) {
        super(props) ;       

        this.s = new Repository() ;
        this.s.addSubscriber( this ) ;

        console.log( this.s.get( 'counter') ) ;       
    }

    render() {
        return (
            <div>
                <h1>
                    Current Counter: {this.s.get( 'counter')}
                </h1>
            </div>
        )
    }
}

CounterButton.jsx
Code:
import React, { Component } from 'react'
import Repository from '../Repository'

export default class CounterButton extends Component {

    constructor(props) {
        super(props) ;

        this.onSubmit = this.onSubmit.bind(this) ;

        this.s = new Repository() ;       
    }

    onSubmit(e) {

        this.s.incrementCounter() ;
        console.log( this.s ) ;
    }

    render() {
        return (
            <div>
                <button onClick={this.onSubmit}>Press Me!</button>               
            </div>
        )
    }
}

Motherfukk this shyt

redux-pattern.png


My shyt is easier imo.

Even this well known youtuber has trouble explaining this overcomplicated shyt. No human can understand the first 5 minutes of this

 

Koli_Kat

Superstar
Supporter
Joined
Jul 3, 2018
Messages
5,267
Reputation
518
Daps
22,173
I'm probably shooting myself in the foot by avoiding redux (seems just as important as react) but I wrote my own state manager using a singleton class. I feel that the whole redux/flux architecture is convoluted bullshyt.

Repository.jsx
Code:
export default class Repository {

    constructor() {

        if( Repository.instance instanceof Repository ) {
            return Repository.instance ;
        }

        this.state = {
            counter: 0
        } ;
      
        this.subscribers = [] ;

        Object.freeze( this.counter ) ;
        Object.freeze( this ) ;
        Repository.instance = this ;
    }

    addSubscriber( component ) {
        this.subscribers.push( component ) ;
    }

    notifySubscribers() {
        this.subscribers.forEach( (value) => {
            value.forceUpdate() ;
        })
    }

    get(key) {
        return this.state[key] ;
    }  

    incrementCounter( n = 1 ) {
        this.notifySubscribers() ;

        this.state.counter += n ;
        return this.state.counter ;
    }
}

CounterDisplay.jsx
Code:
import React, { Component } from 'react'
import Repository from '../Repository'

export default class CounterDisplay extends Component {

    constructor(props) {
        super(props) ;      

        this.s = new Repository() ;
        this.s.addSubscriber( this ) ;

        console.log( this.s.get( 'counter') ) ;      
    }

    render() {
        return (
            <div>
                <h1>
                    Current Counter: {this.s.get( 'counter')}
                </h1>
            </div>
        )
    }
}

CounterButton.jsx
Code:
import React, { Component } from 'react'
import Repository from '../Repository'

export default class CounterButton extends Component {

    constructor(props) {
        super(props) ;

        this.onSubmit = this.onSubmit.bind(this) ;

        this.s = new Repository() ;      
    }

    onSubmit(e) {

        this.s.incrementCounter() ;
        console.log( this.s ) ;
    }

    render() {
        return (
            <div>
                <button onClick={this.onSubmit}>Press Me!</button>              
            </div>
        )
    }
}

Motherfukk this shyt

redux-pattern.png


My shyt is easier imo.

Even this well known youtuber has trouble explaining this overcomplicated shyt. No human can understand the first 5 minutes of this




Redux is hot garbage- too much boilerplate, over-engineered. Try using the context API.
 
Joined
Jan 21, 2013
Messages
3,416
Reputation
514
Daps
7,621
Reppin
NULL
I'm coding a simple genetic algorithm program in javascript and some times the browser crashes lmao

Redux is hot garbage- too much boilerplate, over-engineered. Try using the context API.

It seems that alot of stuff facebook makes is overengineered: React, redux, graphql, etc. VueJs is way more pleasant to work with. And it's accompanying state manager (vuex) is so easy a 5 year old could understand. I've watched several videos on graphql and don't quite 'get it'.
 

Macallik86

Superstar
Supporter
Joined
Dec 4, 2016
Messages
6,506
Reputation
1,372
Daps
21,227
Yeah I like Traversy Media, i also follow CodingPhase,TheRealCasadaro,RealToughCandy,JulioCodes,BlackNightCoding,and a few others.
Randomly came across this suggestion and I've been binging his videos at work. I'm halfway through his journey but breh decided to get a second job so that he could double his salary from $100k to $200K and flip the excess in real estate :ohlawd:
 
Joined
Jan 21, 2013
Messages
3,416
Reputation
514
Daps
7,621
Reppin
NULL
200 line genetic algorithm in c++. Type in a single phrase in all lower case and it tries to guess the target phrase in multiple generations. In each generation it gets better and better:

Code:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <tuple>

#ifndef EXIT_SUCCESS
#define EXIT_SUCCESS 0
#endif

namespace intelligence {

    const unsigned CHARCODE_BEGIN = 97 ;
    const unsigned CHARCODE_END = 122 ;

    class Chromosome {

        std::vector<char> _genes ;

      public :

        Chromosome(): _genes{} {}

        Chromosome( unsigned short geneSize ) {

            for( int i = 0 ; i < geneSize ; i++ )     
                _genes.push_back( CHARCODE_BEGIN + rand() % (CHARCODE_END-CHARCODE_BEGIN+1) ) ;
        }

        float calculateFitness( const std::string& target ) {

            float fitnessScore = 0.0f ;         

            for( int i = 0 ; i < _genes.size() ; i++ ) {

                if( _genes[i] == target[i] )
                    fitnessScore++ ;                 
            }

            return pow( fitnessScore / target.length(), 2 ) ;
            //return fitnessScore / target.length() ;
        }

        Chromosome crossover( const Chromosome& other ) {

            Chromosome child ;
            std::vector<char> newGenes ;         
            int midPoint = rand() % other.size() ;

            for( int i = 0 ; i < this->size() ; i++ ) {
             
                if( i > midPoint )
                    newGenes.push_back( other.getGene( i ) ) ;

                else
                    newGenes.push_back( this->_genes[i] ) ;
            }

            child.setGenes( newGenes ) ;

            return child ;
        }

        void mutate( float mutationRate ) {

            std::replace_if( _genes.begin(), _genes.end(), [mutationRate]( char unused ) {
                return ( ((rand() % 100) * 0.01f) < mutationRate ) ;
            }, CHARCODE_BEGIN + rand() % (CHARCODE_END-CHARCODE_BEGIN+1) ) ;         
        }

        void setGenes( const std::vector<char> genes ) {
            _genes = genes ;
        }

        std::vector<char> getGenes() const{ return _genes ; }
        char getGene( int idx ) const{ return _genes[idx] ; }

        unsigned int size() const{ return _genes.size() ; }
    } ;

    class Population {

        std::vector<Chromosome> _population ;
        unsigned short _generation ;
        unsigned short _mutationRate ;
        std::string _target ;

      public :

        Population( unsigned short popSize, const std::string target, unsigned short mutationRate ): _target(target),
            _mutationRate(mutationRate), _generation(0) {

            for( int i = 0 ; i < popSize ; i++ )
                _population.push_back( Chromosome( target.length() ) ) ;         
        }

        void acceptReject() {

            std::vector<Chromosome> newPopulation ;
            unsigned short maxFitness = getHighestFitnessScore() ;

            for( unsigned int i = 0 ; newPopulation.size() < _population.size() ; i++ ) {         
             
                Chromosome parentA = _population[ rand() % _population.size() ] ;
                Chromosome parentB = _population[ rand() % _population.size() ] ;             
                float randomScore = 0.01f + (rand() % maxFitness) * 0.01f ;

                float parentAScore = parentA.calculateFitness( _target ) ;
                float parentBScore = parentB.calculateFitness( _target ) ;

                if( (parentAScore > randomScore) && (parentBScore > randomScore) ) {
                 
                    Chromosome child = parentA.crossover( parentB ) ;
                    child.mutate( _mutationRate ) ;
                 
                    newPopulation.push_back( child ) ;
                }
            }

            _population = newPopulation ;
            _generation++ ;
        }

        void run() {

            float bestScore = 0.0f ;

            std::cout << "Target: " << _target << '\n' ;

            while( bestScore < 1.0f ) {

                std::tuple<std::string, float> info ;

                info = getHighestFitness() ;
                bestScore = std::get<1>(info) ;
             
                std::cout << "Generation: " << _generation << "; Best Guess: " << std::get<0>(info) << "; Best Score: " << bestScore*100 << "%\n" ;

                acceptReject() ;
            }
        }

        unsigned getHighestFitnessScore() {

            float highest = 0.0f ;

            for( auto itr : _population ) {

                float fitnessScore = itr.calculateFitness( _target ) ;

                if( fitnessScore > highest )
                    highest = fitnessScore ;
            }

            return unsigned(highest * 100) ;
        }

        std::tuple<std::string, float> getHighestFitness() {         
         
            std::vector<char> highestString ;
            float highestScore = 0.0f ;         

            for( auto itr : _population ) {

                float fitnessScore = itr.calculateFitness( _target ) ;

                if( fitnessScore > highestScore ) {
             
                    highestScore = fitnessScore ;
                    highestString = itr.getGenes() ;
                }
            }

            std::string s( highestString.begin(), highestString.end() ) ;

            return std::make_tuple( s, highestScore ) ;
        }     

    } ;
}

void printGenes( const std::vector<char>& genes ) {
    for( auto itr: genes )
        std::cout << itr ;

    std::cout << '\n' ;
}

int main() {

    srand( time(NULL) ) ; 

    std::string target ;
    std::cout << "Enter Target Phrase: " ;
    std::getline( std::cin, target ) ;

    intelligence::Population p( 1500, target, 0.05f ) ;
    p.run() ;

    return EXIT_SUCCESS ;
}
 
Last edited:
Top