Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

Random question

Name: Anonymous 2009-11-25 21:52

Using c++, I have a string vector containing thousands of elements. I know I can erase a specific element using:

vectorName.erase(remove(vectorName.begin(),vectorName.end(),"whatever"),vectorName.end());

In that example it would get rid of any vector elements that equaled "whatever".

I'm looking to erase certain elements if anywhere in that element it has a certain combination of letters. In my case, I want to erase any URLs that make it into the vector, so I would erase any element that contained "http" or "www" in it.

I tried googling this, but I have no clue what to look for. Can anyone offer their expertise? Is there a one line solution using vectorName.erase()?

Name: Anonymous 2009-11-25 22:02

just dont seg fault

Name: Anonymous 2009-11-25 22:09

>>1
vectorName = [s for s in vectorName where "http" not in s and "www" not in s]

Oh wait, sepples...

for (std::vector<std::string>::iterator i = vectorName.begin(); i != vectorName.end(); ++i)
  if (i->find("http") != -1 || i->find("www") != -1)
    vectorName.erase(i--);

Name: Anonymous 2009-11-25 22:27

>>3
@vectorName = grep !/(http|www)/, @vectorName

Oh wait, sepples...

Name: Anonymous 2009-11-25 22:27

>>3

Thanks man!

Name: Anonymous 2009-11-26 2:02

>>3
You're not Thinking in Sepples yet.

#include <vector>
#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <string>

using namespace std;

// If either of the arguments is a reference it can't be used with ptr_fun/bind2nd
// because the workings of binder2nd would form a reference to a reference, which is
// illegal.
bool contains(string s, string t) {
    return s.find(t) != string::npos;
}

template<class A, class B, class Arg>
struct unary_or_function: unary_function<Arg, bool> {
    unary_or_function(A const& f, B const& g)
        : f(f), g(g) { }

    bool operator() (Arg x) const {
        return f(x) || g(x);
    }

    const A& f;
    const B& g;
};

template<class A, class B>
unary_or_function<A, B, typename A::argument_type>
unary_or(A const& f, B const& g) {
    return unary_or_function<A, B, typename A::argument_type> (f, g);
}

int main() {
    vector<string> strings;
    strings.push_back("one");
    strings.push_back("two");
    strings.push_back("three");
    strings.push_back("http://four");
    strings.push_back("www.opera.com");
    strings.push_back("six");
   
    copy(strings.begin(), strings.end(), std::ostream_iterator<string>(cout, "\n"));

    vector<string>::const_iterator new_end =
        std::remove_if(strings.begin(), strings.end(), unary_or(
            bind2nd(ptr_fun(contains), "http"),
            bind2nd(ptr_fun(contains), "www")));
    strings.erase(new_end, strings.end());

    copy(strings.begin(), strings.end(), std::ostream_iterator<string>(cout, "\n"));
}

Name: Anonymous 2009-11-26 2:50

>>6
STRAWMAN MY ANUS

Name: Anonymous 2009-11-26 6:59

>>6
EXPERT

Name: Anonymous 2009-11-26 7:17

>>6
I barf'd.

Name: Anonymous 2009-11-26 12:15

>>6
Someone hasn't updated to TR1.

Name: Anonymous 2009-11-27 13:20

FIBONACCI BUTT SORT

Name: Anonymous 2009-11-27 20:17

vectorName = SELECT name FROM vectorName WHERE name NOT LIKE '%http%' AND name NOT LIKE '%www%'

Name: Anonymous 2009-11-27 22:04

>>6
I want to cry.

Also, somewhat surprised that no one has posted a lisp version yet.

Name: Anonymous 2009-11-27 22:24

>>13
In scheme, I would have probably just used a list instead of a vector so
(require srfi/13)
(filter (lambda (x) (or (string-contains x "http")
                        (string-contains x "www")))
        the-list)

Presumably, I could write an equivalent procedure for vectors, but it would take me a little longer to think of a solution

Name: Anonymous 2009-11-28 7:59

>>13
Here's a more generic solution in CL:

(defun remove-badwords (sequence badwords)
  (remove-if #'(lambda (x)
                 (dolist (s badwords)                  
                   (when (search s x :test #'equalp)
                     (return t))))
             sequence))

CL-USER> (remove-badwords #("abcdef" "http://dis.4chan.org/prog/" "..A..." "huh") '("http" "www" "..."))
#("abcdef" "huh")

Another solution if the badwords are hardcoded, is to replace that DOLIST with (or (search ...) (search ...) (search ...), or just write a macro which does that for you:


(defmacro do-log (op list item &body template)
  `(,op ,@(mapcar #'(lambda (x)
                      `(let ((,item ,x))
                         ,@template))                 
                  list)))

(do-log or ("http" "www" "...") s
  (search s "http://dis.4chan.org/prog/" :test #'equalp))

;;; expands to
(OR
 (LET ((S "http"))
   (SEARCH S "http://dis.4chan.org/prog/" :TEST #'EQUALP))
 (LET ((S "www"))
   (SEARCH S "http://dis.4chan.org/prog/" :TEST #'EQUALP))
 (LET ((S "..."))
   (SEARCH S "http://dis.4chan.org/prog/" :TEST #'EQUALP)))

;;; which is fine, but not perfect, a better solution would be:

(defmacro do-log (op list item &body template)
   `(,op ,@(mapcar #'(lambda (x)                      
                       (eval `(let ((,item ,x)) ,@template)))
                   list)))

(do-log or ("http" "www" "...") s
  `(search ,s "http://dis.4chan.org/prog/" :test #'equalp))

; expands to

;=> (OR (SEARCH "http" "http://dis.4chan.org/prog/" :TEST #'EQUALP)
;       (SEARCH "www" "http://dis.4chan.org/prog/" :TEST #'EQUALP)
;       (SEARCH "..." "http://dis.4chan.org/prog/" :TEST #'EQUALP))

;;; Which is exactly the desired expansion, but it uses EVAL at macroexpansion time, which might be considered a bit distateful. I also wrote a solution which involves nested backquotes, but it's a bit hard to read compared to the EVAL one, so I'm not including it here.
;;; REMOVE-BADWORDS using hardcoded badwords:
(defun remove-badwords (sequence)
  (remove-if #'(lambda (x)
         (do-log or ("http" "www" "...") s
           `(search ,s x :test #'equalp)))
         sequence))

;;; These were simple, and fun exercises, but in a real application, one might want to just use a regular expression:
(require :cl-ppcre) ; or (asdf:oos 'asdf:load-op '#:cl-ppcre)
(defun remove-badwords (sequence badwords)
  (remove-if #'(lambda (x) (cl-ppcre:scan "http|www" x)) sequence))

Name: >>15 2009-11-28 8:17

In the last function, one should remove the badwords keyword as it's hardcoded. Another thing, the SEPPLES version seems to use erase, which is a destructive operation. If that is desired, references to REMOVE-IF should be replaced with DELETE-IF in the code examples in >>15, and the original sequence should be SETF'd with the result of that DELETE-IF.

Name: Anonymous 2009-11-28 8:27

>>13
please don't encourage them

Name: Anonymous 2009-11-28 8:36

>>17
What's the matter? TOO ABSTRACT FOR YOU?
Lisp solutions which were presented were 2,3,6,9 lines long each. Subtract 1 or 2 lines from each (as appropriate) if you don't count function/macro definition. They worked, on lists and arrays, and had different performance characteristics, leaving you a wide choice.

Name: Anonymous 2009-11-28 8:53

>>14
I can't believe it took me this long to notice, but there should have been a not in there.

Name: cOMMUNE LISP FANBOY 2009-11-28 11:04

>>15
That's not a generic solution. Here's a generic solution (which incidentally is must faster).

(defun badword-p (string badwords)
  (not (not (gethash (word string) badwords)))))

(defun remove-badwords (sequence badwords)
  (remove-if #'badword-p sequence))

Name: Anonymous 2009-11-28 11:08


(let ((ht (make-hash-table)))
  (setf (symbol-function 'word) #'identity
        (gethash 'cunt ht) t)
  (remove-badwords '(you are a cunt))

Name: Anonymous 2009-11-28 11:09

>>17
You say that now, but if the Lisp stops, you and I both know that it will only result in more Haskell. Do you want that on your conscience?

Name: Anonymous 2009-11-28 11:24

>>20
That's a fun trick, but treating strings as symbols pollutes the current package with random symbols, it's also limited to entire words separated by spaces, as opposed to badwords which are part of a larger string.

Name: Anonymous 2009-11-28 11:40

>>20
must faster

Name: Anonymous 2009-11-28 13:15

>>23
Lisp knows better than that. >>20 works with both strings and symbols. Here's an example

(defun word (string)
  (string-downcase (string-trim '(#\Space)
                                 (string string))))

(let ((ht (make-hash-table)))
  (setf (gethash (word (read)) ht) t)
  (remove-badwords (read) ht))


with input:

cunt
'("you" "are" "a" "fat" "cunt")


Obvously, it works fine with strings. You can change the second (read) to (cl-ppcre:split " " (read)) or equivalent.

>>24
pointing out typos being the sole contribution >>24 was able to come up with

Name: Anonymous 2009-11-28 16:38

>>23
what's up faggot

Name: Anonymous 2009-11-28 17:57

import Data.List

filterWords infixes words = filter (not . or . (\w -> map (`isInfixOf` w) infixes)) words

Name: Anonymous 2009-11-28 18:00

>>27
flip filter words $ not . or . flip map infixes . isInfixOf

Name: Anonymous 2009-11-28 18:33

>>28
[w | w <- words,  not $ or $ map (`isInfixOf` w) infixes]

Name: Anonymous 2009-11-28 18:44

-fno-monomorphism-restriction

import Data.List
import Control.Monad

filterWords = filter . (.) (not . or) . (.(:[])) . ap . map isInfixOf

Name: Anonymous 2009-11-28 19:46

. (.) (not . or) . (.(:[])) .
Wow. HASKAL really is the new Perl.

Name: Anonymous 2009-11-28 21:17

>>31
You couldn't achieve this much information density in Perl code.

Name: Anonymous 2009-11-28 21:35

>>32
You couldn't achieve this many dicks up your ass in Perl code.

Name: UMH memesmith !gNlkr4vCuc 2009-11-28 22:01

>>31
( ¬_¬)

Name: Anonymous 2009-11-28 22:03

-- superior ruby with regex
badwords = [/http/, /www/]
vectorName.delete_if {|w| badwords.each {|b| w =~ b } }

Name: Anonymous 2009-11-28 22:53

>>32

/;{}def/#{def}def/$_={/Times-Bold exch selectfont}#/_{rmoveto}#/"{dup}#/*/!/$
;/q{exch}#/x ; {/J q #}#/.{/T q #}#{stringwidth}#{}#{}# 14 string dup dup dup
260 40 moveto 90 rotate ; %/}};$0='"\e[7m \e[0m"';@ARGV=split//,reverse
q(ThePerl). q(Journal) x 220 ; q ; 0 T putinterval exch 7 J putinterval ;
 ; $_= q /m$ pop T($*!$"=!$ " )pop " * true% ? $ " $!" "  !!  !! % !" !"    !
! charpath {!"""}pop $ pop{""!}pop ! neg{!#}pop 220 ! neg _{!!}pop J false %T
charpath  clip " pop 0 " moveto 6{!!}pop $_= 105{!!}pop {$ ! $ " !  #! ##}
pop{dup dup $ ! " pop pop q{"}pop 22{dup show}repeat {"}pop q 22 mul{$ "} pop
neg{!#! $ "}pop ! 8 .65 mul{$ # # $}pop ! neg{"}pop  _ pop{"}pop } repeat pop
" {  $ " ! ! ! $ " ! !" "#"  #"!"""""! #" " # "m/;@ARGV=(@ARGV[-14..-1])x50;q}
 0 "%};s/m[ou]|[-\dA-ln-z.\n_{}]|\$_=//gx;s/(.)(?{$*=''})/('$*.='.(++$#
%2?'':"$0;").'pop;')x(ord($1)-31).'$*'/gee;s/((.(\e\[.m)*|.){77})/$1\n/g;print
; sub showpage {}


Valid postscript

Name: Anonymous 2010-12-06 9:19

Back to /b/, ``GNAA Faggot''

Name: Anonymous 2010-12-09 16:00

Name: Anonymous 2013-01-19 14:32

/prog/ will be spammed continuously until further notice. we apologize for any inconvenience this may cause.

Name: Anonymous 2013-09-01 17:03


Zermelo began to work on the problems of set theory under Hilbert's influence and in 1902 published his first work concerning the addition of transfinite cardinals. By that time he had also discovered the so-called Russell paradox. In 1904, he succeeded in taking the first step suggested by Hilbert towards the continuum hypothesis when he proved the well-ordering theorem (every set can be well ordered). This result brought fame to Zermelo, who was appointed Professor in Göttingen, in 1905. His proof of the well-ordering theorem, based on the powerset axiom and the axiom of choice, was not accepted by all mathematicians, mostly because the axiom of choice was a paradigm of non-constructive mathematics. In 1908, Zermelo succeeded in producing an improved proof making use of Dedekind's notion of the "chain" of a set, which became more widely-accepted; this was mainly because that same year he also offered an axiomatization of set theory.

Name: Anonymous 2013-09-01 19:20


Cartesian product of A and B, denoted A × B, is the set whose members are all possible ordered pairs (a,b) where a is a member of A and b is a member of B. The cartesian product of {1, 2} and {red, white} is {(1, red), (1, white), (2, red), (2, white)}.

Don't change these.
Name: Email:
Entire Thread Thread List